Define a function PrintValue() that takes two integer parameters and outputs the sum of all integers starting with the first and ending with the second parameter, followed by a newline. The function does not return any value.
Ex: If the input is 1 4, then the output is:
10
Note: Assume the first integer parameter is less than the second.
#include <iostream>
using namespace std;
void PrintValue()
{
int first, ending;
cout << "Please, input the first and ending parameters: ";
cin >> first >> ending;
double sum = 0;
if (first < ending)
{
for (int i = first; i <= ending; i++)
{
sum += i;
}
cout << "The sum of integers is "<<sum;
}
else
{
cout << "Incorrect input! first must be less than ending!";
}
}
int main()
{
PrintValue();
}
Comments
Leave a comment