Advertisement
MaxTwain

РЕКУРСИЯ(factorial)

Mar 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.37 KB | None | 0 0
  1. #include <iostream>
  2. /*-----------For example: factorial(6) = 1*2*3*4*5*6 = 720-----------*/
  3. using namespace std;
  4.  
  5. int factorial (int f)
  6. {
  7.     if (f == 1)
  8.     {
  9.         return 1;
  10.     }
  11.     else
  12.     {
  13.         return f * factorial(f-1);
  14.     }
  15. }
  16. int main()
  17. {
  18.     int f;
  19.     cin >> f;
  20.     cout << "factorial(" << f << "): " << factorial(f);
  21.     return 0;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement