Advertisement
Josif_tepe

Untitled

Mar 18th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int factorial(int x) {
  6.     if(x == 1) {
  7.         return 1;
  8.     }
  9.     return factorial(x - 1) * x;
  10. }
  11. int main()
  12. {
  13.     int n;
  14.     cin >> n;
  15.     cout << factorial(n) << endl;
  16.     return 0;
  17. }
  18.  
  19. //factorial(5) = 120
  20. // factorial(4) * 5 = 24 * 5 = 120
  21. //factorial(3) * 4 = 6  * 4 = 24
  22. // factorial(2) * 3 = 2 * 3 = 6
  23. //factorial(1) * 2 = 1 * 2 = 2
  24. // factorial(1) = 1
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement