In: Computer Science
Given two integers, start and end, where end is greater than start, write a recursive C++ function that returns the sum of the integers from start through end, inclusive.Example: If start is 5 and end is 10 then the function will return: 45 which is sum of 5, 6, 7, 8, 9, and 10.
int sum (int start, int end){
The Program Code is:
///Cpp Program to sum from Start to
End Integers
#include<iostream>
using namespace std;
int sum(int first, int
last)///Recursive Function Definition
{
if( first == last )
{
return first;
}
else
{
return first + sum(first+1,last);///Recursive logic
}
}
int main()
{
int first ,last,sum1 = 0;
cout<<"Enter First and last Integers :"<<endl;
cin>>first>>last; /// It will Read the Two
Integers
sum1 = sum(first,last);/// Recursive Function calling
cout<<"Sum of the integers
from Start to Last is : "<<sum1;
cout<<endl;
return 0;
}
Dear Student :
This Program given the Solution for your Question
Still you have any Query ?
Fell free to Ask in Comment Section
Thank You