Topic for Statement Description Write a program that finds the sum of one or mor
ID: 3563906 • Letter: T
Question
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. Testing Test Run 1 (User input is in bold). Enter the starting number: 1 Enter the ending number: 5 The sum of numbers from 1 to 5 is 15Explanation / Answer
int main()
{
int a,b,c,d,i;
int sum=0;
cout<<"Enter the starting number:";
cin>>a;
cout<<"Enter the ending number:";
cin>>b;
if(a>b) {c=a;d=b;}
else {c=b;d=a;}
for(i=c;i<=d;i++)
sum=sum+i;
cout<<"The sum of numbers from "<<c<<" to "<<d<<" is "<<sum;
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.