Advertisement
Josif_tepe

Untitled

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