Advertisement
Josif_tepe

Untitled

Jan 12th, 2023
608
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. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. int rec(int number) {
  7.     if(number == 0) {
  8.         return 0;
  9.     }
  10.     int digit = number % 10;
  11.     return rec(number / 10) + digit;
  12. }
  13.  
  14. int main() {
  15.    
  16.    cout <<  rec(1304) << endl;
  17.     return 0;
  18. }
  19. // rec(1304) = rec(130) + 4 = 4 + 4 = 8
  20. // rec(130) = rec(13) + 0 = 4 + 0 = 4
  21. // rec(13)  = rec(1) + 3 = 1 + 3 = 4
  22. // rec(1) = rec(0) + 1 = 0 + 1 = 1
  23. // rec(0) = 0
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement