Write a program that defines and tests a factorial function. The factorial of a number is the product of all whole numbers from 1 to N. For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120. (Hint: Use a for loop as described in Charter 3)
Please help me, my slowly reader is sometimes problem because I still don't get it all. Thanks.
#include <iostream>
usingnamespace std;
int main(int argc, char *argv[])
{
int N;
longlong result = 1;
cout << "Give N = "; cin >> N;
/*
We will calculate RESULT = RESULT * N * N-1 * N-2 * ... * 2 * 1;
The reason for which RESULT must be initialised to 1 is that before initializing
the variable has garbage values, meaning that on that spot on memory there could be
any values stored.
*/
for (int i = N; i >= 1; i--)
{
result = result * i;
}
cout << N << "! = " << result << endl;
return 0;
}