Question: C++ program that sums up the integers in a range of values and prints the result. This will be done two different ways: using a
C++ program that sums up the integers in a range of values and prints the result. This will be done two different ways: using a while loop and using a for loop.
For this assignment, you have more freedom in choosing the local variables that you need to declare and in figuring out what source code to write. However, your program needs to follow the coding standards of this course and fulfill the software requirements described in the next section.
Software Requirements
The program shall follow these steps:
Declare the variables that you will need for this program. When doing so, declare a variable to hold the sum from the "while" loop approach and a variable to hold the sum from the for loop approach
Prompt the user for three integer values, a starting value, an ending value, and an increment value, and read in these values
If the starting value is greater than the ending value, print an error message followed by return 0;, which causes the main function to end and the program to terminate
If the increment value is less than or equal to zero, print an error message followed by return 0;, which causes the main function to end and the program to terminate
Set the sum of the "while" loop approach to zero (This is analogous to pushing Clear on a calculator)
Beginning with the starting value, implement an algorithm that uses a "while" loop to add up the numbers in the given range and increment
Print the sum that results from using the "while" loop approach
Set the sum of the for loop approach to zero
Beginning with the starting value, implement an algorithm that uses a "for" loop to add up the numbers in the given range and increment
Print the sum that results from using the for loop approach. (As a way of testing your two implementations, the two sums should be the same)
Note that the program does not calculate any sums if an error is detected in the input values. It just prints an error message and terminates.
Below is an example execution of the program. In this case, the program added up the numbers 8, 25, 42, 59, 76, 93, and 110. Your program shall follow the same format shown below for prompting the user and printing the results.
Enter a starting integer value: 8 Enter an ending integer value: 121 Enter a positive increment: 17 Sum (using a while loop): 413 Sum (using a for loop): 413
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
