Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int f(int number) {
- if(number == 0) {
- return 0;
- }
- int digit = number % 10;
- return digit + f(number / 10);
- }
- int main() {
- cout << f(1234) << endl;
- return 0;
- }
- // f(1234) = 4 + f(123) = 4 + 6 = 10
- // f(123) = 3 + f(12) = 3 + 3 = 6
- // f(12) = 2 + f(1) = 2 + 1 = 3
- // f(1) = 1 + f(0) = 1 + 0 = 1
- // f(0) = 0
Advertisement
Add Comment
Please, Sign In to add comment