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...
#include<iostream>
usingnamespace 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;
#include<iostream>
usingnamespace 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
#include <iostream>
usingnamespace 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;
}
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.
#include <iostream>
usingnamespace 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.
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..
#include<iostream>
usingnamespace 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.