Advertisement
Josif_tepe

Untitled

Feb 18th, 2025
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <cmath>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int rec(int x) {
  8.     if(x == 0) {
  9.         return 0;
  10.     }
  11.    
  12.     int cifra = x % 10;
  13.     return cifra + rec(x / 10);
  14. }
  15. int main()
  16. {
  17.    
  18.     cout << rec(1234) << endl;
  19.  
  20.     return 0;
  21. }
  22. // rek(1234) = 4 + rec(123) = 4 + 6 = 10
  23. // rec(123) = 3 + rec(12) = 3 + 3 = 6
  24. // rec(12) = 2 + rec(1) = 2 + 1 = 3
  25. // rec(1) = 1 + rec(0) = 1 + 0 = 1
  26. // rec(0) = 0
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement