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