Advertisement
Josif_tepe

Untitled

May 12th, 2025
273
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. using namespace std;
  3.  
  4.  
  5. int rec(int x) {
  6.     if(x == 0) {
  7.         return 0;
  8.     }
  9.     int cifra = x % 10;
  10.    
  11.     return rec(x / 10) + cifra;
  12. }
  13. int main() {
  14.     int r = rec(1234);
  15.     cout << r << endl;
  16.     return 0;
  17. }
  18. // rec(1234) = rec(123) + 4 = 6 + 4 = 10
  19. // rec(123) = rec(12) + 3 = 3 + 3 = 6
  20. // rec(12) = rec(1) + 2 = 1 + 2 = 3
  21. // rec(1) = rec(0) + 1 = 0 + 1 = 1
  22. // rec(0) = 0
  23.  
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement