Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. input = 5;
  2. 5! = 5(4)(3)(2)(1)
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. int fac = 0;
  11. int sum = 0;
  12. int cnt = 1;
  13. cout << "Input a number to get the Factorial: ";
  14. cin >> fac;
  15.  
  16. for (fac; fac > 0; cnt ++)
  17. {
  18. sum = fac(fac - cnt)
  19. }
  20.  
  21. cout << fac << "! : ";
  22. }
  23.  
  24. sum = fac * (fac - cnt)
  25.  
  26. for( fac; fac > 0; cnt++ )
  27. {
  28. sum = fac * (fac - cnt);
  29. }
  30.  
  31. int prod = 1;
  32. for( i = fac; i > 0; --i ) // <-- notice the --i
  33. prod = prod * i; // <-- equiv to prod *= i;
  34.  
  35. prod i new_prod
  36. ---------------------------
  37. 1 5 1*5 = 5
  38. 5 4 5*4 = 20
  39. 20 3 20*3 = 60
  40. 60 2 60*2 = 120
  41. 120 1 120*1 = 120
  42. 120
  43.  
  44. cout << fac << "! = " << prod << endl;
  45.  
  46. int fac(int n)
  47. {
  48. return (n == 1 || n == 0) ? 1 : fac(n - 1) * n;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement