How to Write a function factorial

May 9, 2011 at 11:03pm
The question is "Write a function factorial, which calculates the factorial of an integer. "
so how can i write this function
please aid me!
May 9, 2011 at 11:18pm
closed account (D80DSL3A)
Try searching this site for "factorial function" or similar.
The problem has appeared countless times on this site.
May 10, 2011 at 3:15am
this is the classic example of a program where recursion is the most efficient way to calculate it. also im pretty sure C++ starts to be incorrect starting at around 15! or so.

hint: the base case is when you have to multiply by 1 :)
May 10, 2011 at 3:51am
Can you define mathematically say 9!
May 10, 2011 at 10:00am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

int factorial (int x);

int main () 
{

	int n;
		cout<<"Enter number: ";
		cin>>n;

		int fact;

		fact = factorial(n);

		cout<<"The factorial of number: "<<n<<" is: "<<fact<<endl;

		cin.get(); cin.get();
		return 0;
}

int factorial(int x)
{

int i;
int F=1;

for (i = 1; i <= x; i++)
	F*=i;

	return F;
}
May 10, 2011 at 10:10am
@ascii

...C++ starts to be incorrect starting at around 15.

That is wrong statement. Its not C++ that is incorrect, you must use type that can accept/hold large numbers. See (for example) this: http://www.cplusplus.com/reference/std/limits/numeric_limits/
Make your own type that can represent these numbers or use some library for this purpose.
Last edited on May 10, 2011 at 10:12am
May 10, 2011 at 8:26pm
:o thank you thats an interesting class :)
May 10, 2011 at 8:53pm
Here is a factorial code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib>
#include <iostream>

using namespace std;

int factorial(int x)
{
   int y=1 ;
   int z=x ;
   int fact ;
   for (x=x-1; x>0; x--)
   {
      y=y*x ;
   }
   fact=y*z ;
   return fact ;
}
int main()
{
   int n ;
   cout<<"Enter a number: " ;
   cin>> n ;
   cout<<n <<"!=" <<factorial(n) <<endl ;
   system("PAUSE") ;
}


May 10, 2011 at 9:30pm
Why so complicated? :)
May 10, 2011 at 9:41pm
Thats not complicated, its short simple and to the point.
May 10, 2011 at 9:52pm
as i pointed out before this excersise is best down with a recursive function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int factorial(int number)
{
	int fact = number;
	if (number == 1)
		return 1;
	else
		return fact * factorial(number - 1);
}

int main(void)
{
	int number;
	cout << "Enter a number: " << endl;
	cin >> number;
	cout << factorial(number);

	cout << endl;
	return 0;
}


this is far more efficient :)

also doesnt use system commands :o
Last edited on May 10, 2011 at 9:53pm
May 10, 2011 at 10:08pm
recursive function is better. Sorry i forgot about it
May 11, 2011 at 5:56am
@mcqueen ...no it's not. Try getting the value of 51! or even 27!
May 11, 2011 at 8:36pm
whats your point buffbill? a recursive function is the most efficient. factorial 27 is probably out of the range of an int anyways, and 51 certainly is.
May 11, 2011 at 9:37pm
Using recursion is the standard way but since you've done the OP's homework for him and he's probably gone, what's the point of the debate???
May 11, 2011 at 10:04pm
debating the most effective method?
Last edited on May 11, 2011 at 10:15pm
May 11, 2011 at 10:56pm
1
2
3
4
5
6
7
typedef unsigned long int ulong;
ulong factorial(ulong num)
{
	if (1 >= num)
		return 1;
	return num * factorial(num - 1);
}


even this is better for big numbers up to 65:
 
typedef unsigned long long int ulong;
Last edited on May 11, 2011 at 11:08pm
May 11, 2011 at 11:17pm
good idea :)
May 12, 2011 at 1:06am
Ok, this is it:

1
2
3
4
5
6
7
typedef whatever_you_want fact_type;
fact_type factorial(fact_type n) //I HATE camel casing for functions.  How come you guys like it so much???
{
    if (n == 0) return 1;
    if (n < 3) return n;
    return n * factorial(n -1);
}
Topic archived. No new replies allowed.