How to Write a function factorial

The question is "Write a function factorial, which calculates the factorial of an integer. "
so how can i write this function
please aid me!
closed account (D80DSL3A)
Try searching this site for "factorial function" or similar.
The problem has appeared countless times on this site.
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 :)
Can you define mathematically say 9!
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;
}
@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
:o thank you thats an interesting class :)
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") ;
}


Why so complicated? :)
Thats not complicated, its short simple and to the point.
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
recursive function is better. Sorry i forgot about it
@mcqueen ...no it's not. Try getting the value of 51! or even 27!
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.
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???
debating the most effective method?
Last edited on
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
good idea :)
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.