Answer to Question #328593 in C++ for Program Training

Question #328593

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.




1
Expert's answer
2022-04-16T06:48:45-0400
#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();
}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS