Write a method called SumRange that accepts two integer parameters that form a r
ID: 3930706 • Letter: W
Question
Write a method called SumRange that accepts two integer parameters that form a range of numbers. You may assume that the first parameter is the lower or lesser number and that the second parameter is a higher or greater number. The method should use a for loop to sum up all of the numbers between and including the numerical first and the second parameter values. This method should also return the summed value to its calling statement. For example: numbers 1 and 5 as the parameter values would sum up 1 + 2 + 3 + 4 + 5 to produce a returned value of 15.
Explanation / Answer
#include<iostream>
using namespace std;
int sumRange(int first,int second)
{
int i;
int sum = 0;
for(i=first;i<=second;i++)
sum = sum+i;
return sum;
}
int main(){
int sum = sumRange(1,5);
cout << "Sum of the range is : " << sum << endl;
return 0;
}
OUTPUT:
Sum of the range is : 15
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.