Help with sum of numbers

Hey all i'm trying to add the sum between two numbers and for some reason my calculations are off. For example when the user inputs the numbers 1 and 4 the answer is calculate as 10 instead of 5. I am not sure what I'm doing wrong...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;
 
int main()
{
 
  int num1, num2;
 
  cout<<"Enter an integer: ";
  cin>>num1;
  cout<<"Enter another integer: ";
  cin>>num2;
 
  int total = 0;
  for (int x=num1; x<=num2; x++)
  {
 
         total += x;
 
  }  
 
  cout<<"The sum of the integers between the two numbers is "<< total;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;
 
int main()
{
 
  int num1, num2;
 
  cout<<"Enter an integer: ";
  cin>>num1;
  cout<<"Enter another integer: ";
  cin>>num2;
 
  int total = num1+num2;
  //for (int x=num1; x<=num2; x++)
  //{
 
        // total += x;
 
 // }  
 
  cout<<"The sum of the integers between the two numbers is "<< total;

  //add pause here 

//ico 


this all you need the for statement is useless
I think this what you want to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int X = 0 , Y = 0 , Total = 0;
  cout << "Enter an integer." << endl;
  cin >> X;
  cout << "Enter an other integer." << endl;
  cin >> Y;
  Total = X + Y;
  cout << "The sum of the integers is " << Total << endl;
  return 0;
}


I do not understand why you have a for loop.

Hope this helps :)
Hey thanks for the reply. I don't think I was very clear in my original post. First, I am required to use a for loop for this and I'm trying to find the sum of the integers BETWEEN the two user input values. For example, if the user enters 6 and 10 I want to find the sum of the numbers 7+8+9.
Do you mean this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int X = 0 , Y = 0 , Total = 0;
  cout << "Please enter an integer." << endl;
  cin >> X;
  cout << "Please enter an other integer." << endl;
  cin >> Y;
  for (int i = X ; i <= Y ; i++)
    {
      cout << Total << endl;
      Total += i;
    }
  cout << "The sum between the numbers is " << Total << "." << endl;
  return 0;
}
When the numbers are plugged into the code you have provided the sum between those numbers is incorrect just like my program. I'm not sure what I'm doing wrong I feel like this should be easy and I am making it way too difficult.
@psp1202

I am confused. In your original post, you wrote 1 and 4 the answer is calculate as 10, which is 1+2+3+4, but in the last post, you write, enters 6 and 10 I want to find the sum of the numbers 7+8+9. Which is it? The numbers from start to end, or the numbers in between? Here's your code for either way..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>

using namespace std;
 
int main()
{
   int num1, num2, total;
 
  cout << "Enter an integer: ";
  cin >> num1;
  cout<<"Enter a larger integer: ";
  cin >> num2;
 
  total = 0;
  for (int x=num1; x<=num2; x++)
  { 
        total += x;
  }  
 
  cout<<"The sum of the integers from " << num1 << " to " << num2 << " = " << total << endl;

 total = 0;
  for (int x=num1+1; x<num2; x++)
  { 
        total += x;
  }  
 
  cout<<"The sum of the integers between the numbers = " << total << endl;
return 0;
}

Thank you for the help and sorry for the confusion. When I said the the calculation was equal to 10 I was stating that is what my program was calculating instead of the right answer which would be 5.
i wrote a program like this already
@psp1202

Okay. So then the answer would be the second solution in the code I posted.
Topic archived. No new replies allowed.