Advertisement
Josif_tepe

Untitled

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