Factorial issue

I'm not going to post the whole code since it's pretty long but I'll give a few snips of the important stuff.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main()
{
	string ch;
	int num, num1, num2, num3, num4, dividend, divisor, N, factorial;
	float num6, num7, num8, num9, max, M;

...lots of code in between....
1
2
3
4
5
6
7
	else 
	{
		cout << "\nEnter the factorial number: ";
		cin >> num;
		factorial = (num * num) + (num * ((num + 1) - 1));
		cout << "The factorial of " << num << " is " << factorial;
	}

Now I'm having the issue of getting this down. For example, I want the exponential of 5 when I put it in to be 125. Any suggestions? I tried doing for statements and if statements but I could never figure this out.
Are you trying to calculate 5! (5 factorial) ?

Take a look at your formula; 5*5 + (5*((5+1)-1)) is just 25 + 5.

Remember that n! is n*(n-1)*(n-2)*...*1. A for loop would work:

1
2
3
4
for (int i = num, i > 1, i--)
{
// multiply factorial by i for each iteration through the loop - the loop will end once it hits 1. 
}


I'm trying to calculate where the user puts in any random value so it can calculate #!
Don't mind this here... I just felt like showing off...

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
34
#include <iostream>
using namespace std;

template <int N>
class Factorial
{
public:
    static const int RESULT=N*Factorial<N-1>::RESULT;
};

template <>
class Factorial<0>
{
public:
    static const int RESULT=1;
};

int main()
{
    cout << Factorial<1>::RESULT << endl;
    cout << Factorial<2>::RESULT << endl;
    cout << Factorial<3>::RESULT << endl;
    cout << Factorial<4>::RESULT << endl;
    cout << Factorial<5>::RESULT << endl;
    cout << Factorial<6>::RESULT << endl;
    cout << Factorial<7>::RESULT << endl;
    cout << Factorial<8>::RESULT << endl;
    cout << Factorial<9>::RESULT << endl;
    cout << Factorial<10>::RESULT << endl;

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}

I agree with pabloist. It's easy to calculate n! using a for loop.
Another way to do it would be using a recursive function:

1
2
3
4
5
int factorial(int n)
{
    //if n==0 the result is 1
    //else the result is n*(n-1)!
}
Last edited on
Just a side question, who like recursion a lot ? I notice some programmers possibly used other highly recursive programming language before coming to C++ will use their habits in C++. I maintain ppl code and some developer has some "fanatical" obsession to change snippet of codes into recursive style when sometimes a simple for loop will suffice :O
1
2
3
int factorial(int n) { //please pass in n >= 0
 return (n==0) ? 1 : n * factorial(n-1);
}


I wonder if the iteration version would have more statements than a recursive version? Anyone keen to write an iteration version that is shorter than a recursive version ?
Truth is that in this case (factorial) an iterative approach would be better (both faster and cheaper) than a recursive one. It's also true that there are many cases where people use recursion even if iteration works better. I believe the reason is that recursive code is more readable and closer to how a human would solve the problem intuitively. However there are also cases where recursion practically is the only choice (e.g. quicksort, ackerman function etc...)
Hey everyone,

I talked to my teacher today and she said it would be this:
1
2
3
4
5
6
7
8
9
10
11
	else 
	{
		cout << "\nEnter the factorial number: ";
		cin >> num;
		int factorial = 1;
		for(int i = 1; i <= num; i++)
		{
			factorial *= i;
		}
	   	cout << "The factorial of " << num << " is " << factorial;
	}


Thanks for all the suggestions and pabloist, you were very close. Thanks everyone!
Topic archived. No new replies allowed.