Advertisement
Josif_tepe

Untitled

Oct 3rd, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <fstream>
  5. #include <cmath>
  6. using namespace std;
  7. int rec(int number) {
  8.     if(number == 0) {
  9.         return 0;
  10.     }
  11.     return rec(number / 10) + (number % 10);
  12. }
  13. int main() {
  14.     cout << rec(9145) << endl;
  15.     return 0;
  16. }
  17.  
  18.  
  19. // rec(9145) = rec(914) + 5 = 14 + 5 = 19
  20. // rec(914) = rec(91) + 4 = 10 + 4 = 14
  21. // rec(91) = rec(9) + 1 = 9 + 1 = 10
  22. // rec(9) = rec(0) + 9 = 0 + 9 = 9
  23. // rec(0) = 0
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement