Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- input = 5;
- 5! = 5(4)(3)(2)(1)
- #include <iostream>
- using namespace std;
- int main()
- {
- int fac = 0;
- int sum = 0;
- int cnt = 1;
- cout << "Input a number to get the Factorial: ";
- cin >> fac;
- for (fac; fac > 0; cnt ++)
- {
- sum = fac(fac - cnt)
- }
- cout << fac << "! : ";
- }
- sum = fac * (fac - cnt)
- for( fac; fac > 0; cnt++ )
- {
- sum = fac * (fac - cnt);
- }
- int prod = 1;
- for( i = fac; i > 0; --i ) // <-- notice the --i
- prod = prod * i; // <-- equiv to prod *= i;
- prod i new_prod
- ---------------------------
- 1 5 1*5 = 5
- 5 4 5*4 = 20
- 20 3 20*3 = 60
- 60 2 60*2 = 120
- 120 1 120*1 = 120
- 120
- cout << fac << "! = " << prod << endl;
- int fac(int n)
- {
- return (n == 1 || n == 0) ? 1 : fac(n - 1) * n;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement