Saturday, August 17, 2013

Given two integer start and end. Find the sum start to end(inclusive) using recursion.

#include <iostream>
using namespace std;
int sum(int start, int end){
    if(start>end) return 0;
    return start + sum(start+1, end);
}
int main()
{
    int start, end;
    while(cin>>start>>end){
        cout<<sum(start, end)<<endl;
    }
    return 0;
}

No comments:

Post a Comment