C++ program that sums up the integers in a range of values and prints the result. The computation will be done using two different methods: a while loop approach and a for loop approach. Prompt the user for the starting integer value, the ending integer value, and the integer increment between the two values, and read in these values. Print the sum for the while loop approach and the for loop approach.
Ok those are the directions, and with that I have this so far...
#include <iostream>
#include <stdlib.h>
using namespace std;
// ############################################################
int main(void)
{
int while_sum;
int for_sum;
cout << "First value: ";
int n1;
cin >> n1;
cout << "Ending value: ";
int n2;
cin >> n2;
cout << "Increment value: ";
int n3;
cin >> n3;
if (n1 > n2)
cout << "Error message";
return 0;
if( n3 <= 0)
cout << "Error message";
return 0;
while_sum = 0;
for (
int sum=0;
int c=n1;
while_sum(c < n2)
{
sum+=c;
c+=n3;
}
return 0;
} // End main
Is there anything that I am missing? The steps are here
1.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
2.Prompt the user for three integer values, a starting value, an ending value, and an increment value, and read in these values
3.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
4.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
5.Set the sum of the while loop approach to zero (This is analogous to pushing Clear on a calculator)
6.Beginning with the starting value, implement an algorithm that uses a while loop to add up the numbers in the given range and increment
7.Print the sum that results from using the while loop approach
8.Set the sum of the for loop approach to zero
9.Beginning with the starting value, implement an algorithm that uses a for loop to add up the numbers in the given range and increment
10.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
I have done all up to 6, and that's where I'm stuck. Is my while loop formed correctly?
Hello. I have added your suggestion to the for loop. But I ran the program but I'm still not getting corrects values. I don't think the program is doing what the program is asking. Am i doing something wrong?
int main(void)
{
int while_sum;
int for_sum;
cout << "Enter a starting integer value: ";
int n1;
cin >> n1;
cout << "Enter an ending integer value: ";
int n2;
cin >> n2;
cout << "Enter a positive Increment value: ";
int n3;
cin >> n3;
cout<< "Sum (while loop)";
if (n1 > n2)
cout << "Error message";
return 0;
if( n3 <= 0)
cout << "Error message";
return 0;
while_sum = 0;
for(int i = 0; i < 5; i++) // start at 0, increment i till 4 the drops out of for loop
cout<< "Sum (for loop)";
int sum=0;
int c=n1;
int(c < n2);
{
for_sum+=c;
c+=n3;
}
As Texan said in his/her post, when your program hits your first "return 0;" your program will end. It looks like you still haven't put it in braces so as to include it in your error message if statements, so it will always exit.
It works, but now I'm not getting the correct values, like I don't get values for the sum(while loop) nor the sum of the for loop. Is there a way to get those values?
I see, but when i go compile it, I still don't get the correct values after the for and while loops. here is an example of the values
Enter a starting integer value: 8
Enter an ending integer value : 121