Advertisement
Josif_tepe

Untitled

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