I have to use a loop to find the sum between two integers that the user chooses. The integers can be negative. Im having trouble as I am a very new user to C++.
Thanks!!
#include<iostream>
usingnamespace std;
int main()
{
int number1, number2, sum;
cout << "Input two integers: ";
cin >> number1 >> number2;
sum = 0;
for (int i = number1; i <= number2; i++)
{
sum += i;
}
cout << "Sum of the values from " << number1 << " to " << number2 << " is: " << sum << endl;
sum = 0;
for (int i = number1 + 1; i<number2; i++)
{
sum += i;
}
return 0;
}
Input two integers: -9 4
Sum of the values from -9 to 4 is: -35
And for printing the sum, you can just put a cout statement in your for loop that prints the current number and a plus sign.
(You'll need an extra check for the last number to make sure you don't print a plus sign after that)
Hi long double main! I'm still supposed to be bobthezealot, but my account got hacked, and I can't log on. I made a new account! Nice to see you again!
@ The question
Psst...
You put the letter a at the end of your post!