Saturday, August 17, 2013

Given an integer N. Write a function which will return sum of its digit using Recursion.

#include <iostream>
using namespace std;
int digit_sum(int n){
    if(n == 0) return 0;
    return n%10 + digit_sum(n/10);
}

int main()
{
    int n;
    while(cin>>n){
        cout<<digit_sum(n)<<endl;
    }
    return 0;
}

No comments:

Post a Comment