Josif_tepe

Untitled

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