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