Question: *** USING C++ *** Topic for Statement Description Write a program that finds the sum of one or more consecutive whole numbers. The program asks
*** USING C++ ***
Topic
for Statement
Description
Write a program that finds the sum of one or more consecutive whole numbers. The program asks the user for the starting whole number and the ending whole number. It displays the sum of all numbers starting with the first number and ending with the last number. For example, for a starting number of 3 and an ending number of 7, the sum of 3+4+5+6+7 is 25.
If the second number is smaller than the first number, the program switches the two numbers.
Use a For loop to do the assignment.
Test Run 1
Enter the starting number:
1
Enter the ending number:
5
The sum of numbers from 1 to 5 is 15
Test Run 2
Enter the starting number:
3
Enter the ending number:
7
The sum of numbers from 3 to 7 is 25
Test Run 3
Enter the starting number:
7
Enter the ending number:
3
The sum of numbers from 3 to 7 is 25
Sample Code
//switching the values of startNum and endNum
if (startNum > endNum)
{
int temp;
temp = startNum;
startNum = endNum;
endNum = temp;
}
//For loop
sum = 0;
for (count=startNum; count<=endNum; count++)
{
sum = sum + count;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
