This program will calculate the sum of integer values. Write a program that asks
ID: 3819126 • Letter: T
Question
This program will calculate the sum of integer values.
Write a program that asks the user for a positive integer value (between 1 and 1000)
and that uses a loop to validate the input. The program should then use a second
loop to compute the sum of all the integers from 1 to the number entered.
For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ...,50.
*/
#include <iostream>
using namespace std;
int main()
{
// TODO STEP 1: Declare 3 integer type variables:
// - type int, named sumTo, to hold the user's entry of what to sum up to
// - type int, named count, to serve as the loop control variable when summing
// - type int, named total, to accumulate the sum
//
// Initialize the total accumulator to 0.
// TODO STEP 2: Prompt the user and get the number to sum up to (the sumTo)
// (in the fashion demonstrated in the SAMPLE OUTPUT below).
// TODO STEP 3: Write a loop to validate that the user entered a number between 1 to 1000
// Display an error message if the user made an incorrect entry and prompt them to make
// a new entry. Be sure to use the type of loop BEST suited for this circumstance, where
// the user may make zero or more mistakes (though the exact number is not known) before
// finally entering a value that falls in the correct range. Once again, refer to the
// SAMPLE OUTPUT below to determine the correct formatting of the error message.
// TODO STEP 4: Write a loop to sum from 1 to the number the user entered (sumTo).
// You will need to use the variable named 'total' to accumulate the sum and the variable
// named 'count' will be used as the loop control variable. Be sure to use the type of
// loop BEST suited for this circumstance, where the number of times the loop needs to
// execute IS known before the loop starts. The number of times the loop iterates may
// not be known before the program is run, but it IS known before the loop executes (and
// the value should be stored in a variable if you did the steps above correctly).
// TODO STEP 5: Display the results to the user (in the fashion demonstrated in the
// SAMPLE OUTPUT below).
return 0;
}
/*
************************* SAMPLE OUTPUT #1 *******************************
Enter a positive whole number (no larger than 1000): -7
ERROR: Enter a positive integer value (no larger than 1000): 0
ERROR: Enter a positive integer value (no larger than 1000): 1001
ERROR: Enter a positive integer value (no larger than 1000): 100
The sum of numbers up to 100 is 5050.
Press any key to continue . . .
************************* SAMPLE OUTPUT #2 *******************************
Enter a positive whole number (no larger than 1000): 50
The sum of numbers up to 50 is 1275.
Press any key to continue . . .
*/
Explanation / Answer
#include <iostream>
using namespace std;
int main ()
{
int sum = 0;
int num;
cout << "Enter a positive whole number (no larger than 1000):. ";
cin >> num;
if (num >= 1 && num<1000){
for (int counter = 0; counter <= num; counter++)
{
sum = sum + num;
counter++;
}
cout << "The sum of numbers upto " << num << " " << "is " << sum << endl;
} else if(num>1000) {
cout << " ERROR: Enter a positive integer value (no larger than 1000). ";
} else {
cout << " " << num << " is an invalid input.";
cout << " ERROR:Enter a positive whole number greater than 0. ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.