Hey all, I'm trying to write a factorial function in c++, but I seem to be having some problems. When i pass in the value 5 to my factorial function, i get the result: 2686740 LOL, i'm not sure what's wrong. Any tips to solving this is much appreciated! Thanks.
Nevermind guys! lolol i forgot to return x from the function......
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <iomanip>
usingnamespace std;
int factorial(int x) {
for(int i = x; i > 1; i--)
{
x = x * (i - 1);
}
}
int main()
{
int number;
number = factorial(5);
cout << number << endl;
system("pause");
return 0;
}