Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #include <cmath>
- using namespace std;
- int rec(int number) {
- if(number == 0) {
- return 0;
- }
- return rec(number / 10) + (number % 10);
- }
- int main() {
- cout << rec(9145) << endl;
- return 0;
- }
- // rec(9145) = rec(914) + 5 = 14 + 5 = 19
- // rec(914) = rec(91) + 4 = 10 + 4 = 14
- // rec(91) = rec(9) + 1 = 9 + 1 = 10
- // rec(9) = rec(0) + 9 = 0 + 9 = 9
- // rec(0) = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement