Advertisement
Josif_tepe

Untitled

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