Question

In: Computer Science

Given two integers, start and end, where end is greater than start, write a recursive C++...

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){

Solutions

Expert Solution

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


Related Solutions

Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers....
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers. If x=2, y=4, the method pow should return 16. Java answers only please.
Counting integers greater than 10 Write the pseudo-code for a brute force approach to counting the...
Counting integers greater than 10 Write the pseudo-code for a brute force approach to counting the number of integers greater than 10 in an array of n integers. Write the pseudo-code divide-and-conquer algorithm for counting the number of integers greater than 10 in an array of n integers. Give the recurrence relation for the number of comparisons in your divide-and-conquer algorithm in part b.  
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based...
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based on the fact that xy = 2(x · (y/2)) when y is even and xy = 2(x · ⌊y/2⌋) + x when y is odd, together with the initial condition xy = 0 when y = 0.
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
Prove (Z/mZ)/(nZ/mZ) is isomorphic to Z/nZ where n and m are integers greater than 1 and...
Prove (Z/mZ)/(nZ/mZ) is isomorphic to Z/nZ where n and m are integers greater than 1 and n divides m.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT