i am trying to make a program where a user inputs two numbers. the first number needs to be less then the second. then the program needs to add all the numbers together that fall between those two numbers including the two numbers. for example number1 = 1, number 2= 3.
the program need to output the sum of 1+2+3.

so far all i have is and i dont know what else to do.

#include <iostream>
#include <conio.h>
using namespace std;
int main()
 {
	 int n1;
	 int n2;
	 int total;

	 cout<<"enter n1: ";cin>>n1;
	 cout<<"\nenter n2: ";cin>>n2;

	 while (n1<=n2)
	 {
		 n1=n1+(n1+n2);
	 }
	 
	 cout<<"\ntotal is: "<<n1;
	getch();
}

Recommended Answers

All 14 Replies

1) Use a for loop. It is made to go from one number up to a second number.

2) Don't use conio.h functions. They are not standard and are unnecessary. There are standard functions to accomplish the same thing.

3) In the loop, simply add the current number to the answer, which starts at 0.

i am trying to make a program where a user inputs two numbers. the first number needs to be less then the second. then the program needs to add all the numbers together that fall between those two numbers including the two numbers. for example number1 = 1, number 2= 3.
the program need to output the sum of 1+2+3.

so far all i have is and i dont know what else to do.

#include <iostream>
#include <conio.h>
using namespace std;
int main()
 {
	 int n1;
	 int n2;
	 int total;

	 cout<<"enter n1: ";cin>>n1;
	 cout<<"\nenter n2: ";cin>>n2;

	 while (n1<=n2)
	 {
		 n1=n1+(n1+n2);
	 }
	 
	 cout<<"\ntotal is: "<<n1;
	getch();
}

You need to use your third totalvalue to accumulate the sum. That is, add n1 to it on every step. Also, on every step of the loop increment n1 by 1 ( n1++ ). Finally, you need to be careful about how the loop terminates. Does the sum include n2? If not, your termination expression needs to be n1<n2 You're close, but still have a some work to do.

thank you for the reply, i will try it and will let you know abt progress and trouble

ok so i still have no idea how to do this. alittle more help would be appreciated.

Try this:-
total=0;
for( ;n1<=n2;n1++)
total+=n1;

ok so i still have no idea how to do this. alittle more help would be appreciated.

Help requires a question that can be answered. Details about what you need help with, and the current state of the code.

well i want to use a while loop to do this. i have no idea how to add the numbers. my while loop is wrong and i dont know how to go about doing this.

See if the concepts here can help you:

number1 and number2 are input.

i = number1;
val = 100;
while (i > number2)
{
    val = val - i;
    i = i - 2;
}

What does this code do?

i = number1;
val = 100;
while (i > number2)
 {
    val = val - i;    
    i = i - 2;
 }

while number1 is greater than number 2, subtract number 1 from val (100) and give number one the value of number1 - 2.

Now use that ides to do your program. It only takes a few changes.

i = number1;
val = 100;
while (i > number2) 
{    
      val = val - i;        
      i = i - 2; 
}

why cant i just put this?:

while (number1 > number2)
 {
     val = val - number1;
     number1 = number1 -2;

}

I don't know? Why can't you? When you tried it did you get the correct answer?

thank you so much for the help. took me so long but i finally got it.

i = n1;
	 val = 0;
	 while (i < n2) 
	 {    
		 val = val + i;        
	 	 i = i + 1; 
	 }
	  val = val+n2;
it works
commented: Congrats! +19

Bingo!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.