# while looping

• 12-01-2001
learnin2program
while looping
I am trying to figure out how to make a program so that when I enter a starting number and an ending number all numbers between that are added up.

For Example
First Number: 1
Second Number: 5

The program will: 1+2+3+4+5 = answer

This is what I have so far

#include<iostream.h>
#include<math.h>

main()
{

cin>>firstnum;
cin>>secondnum;

while(firstnum<=secondnum)
{
{
}
} return 0;
}
This prints
1
2
3
4
5

How would I get it to 1 + 2 + 3 + 4 + 5 using the while loop?

Any help would be appreciated.

Thank You

marie10913@aol.com
• 12-01-2001
g
using a for loop it would be

for(i = startingNumberp; i <= endingNumber; i++)
{
}
• 12-01-2001
C_Coder
This should do the trick.
Code:

``` answer = 0; while(firstnum <= lastnum)     answer += firstnum++;```
• 12-01-2001
qwertiop
maybe this will work,i didn't test it though, it may be wrong.

int total=firstnum;
while(firstnum<=secondnum) {
}
cout<<" = "<<total;
• 12-01-2001
learnin2program
still trying to figure out while loop
I tried correcting the while loop... I am a step closer but not quite there....

The code I now am using is

#include<iostream.h>
#include<math.h>

main()
{

cin>>firstnum;
cin>>secondnum;

int total = firstnum;
while(firstnum<=secondnum)
{

}
cout<<" = "<<total;
}

When I run the program I get..

Please enter beginning term: 1 // i entered the number 1
Please enter ending term: 5 // i entered the number 5
5368726784 + 1 + 2 + 3 + 4 + = 16

I'm not sure why I am getting 5368726784.. any suggestions would be appreciated...

when the program is correct it should say 1 + 2 + 3 + 4 + 5 = 15

I apprecate the help so far... Any more would be even more appreciated.

Thank You

Marie10913@aol.com
• 12-01-2001
Unregistered
use this idea for the loop
int total=0
int temp=firstnum;
while(firstnum<=lastnum)
{
cout<<temp<<"+";
total=(total+temp);
temp++;
}
cout<<"="<<total;
• 12-01-2001
learnin2program
I tried the last suggestion unfortuantely I it was never ending... information with I believe six digits plus six digits just kept continuing... this is the code I used.

#include<iostream.h>
#include<math.h>

main()
{
int total = 0;

cin>>firstnum;
cin>>secondnum;

temp = firstnum;
while(firstnum<=secondnum)
{
cout<<temp<<" + ";
total=(total+temp);
temp++;
}
cout<<" = "<<total;
}

Any additional help would be appeciated... I still have some hope that I will end up being able to enjoy a little of the weekend.

Thank You

Marie10913@aol.com
• 12-02-2001
qwertiop
this should work:

main()
{
int total = 0;