0

im trying to learn c++, so i stumble upon this programming exercise that asks the user to make a program that asks for 2 numbers, assuming that the second number is always the biggest in value, and adds all of the numbers in between the numbers entered including the entered numbers.

i figured that using a for function would be nice but im stuck as to what to make the for function do. here is the current code:

#include <iostream>

int main(void)
{
using namespace std;
int no1;
int no2;

cout << "Enter the first number:";
cin >> no1;
cout << "Enter the second number:";
cin >> no2;

for (int count = 0; count != no2; count++)

return 0;

}

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by katokato
0

First off, I'd write the for statement as this:

for (int count = no1; count <= no2; count++)

Notice that the "no1" thing is required and the "<=" thing is my preference.

Now, you want the sum of all numbers between no1 and no2. Inside the body of the "for" loop, it is guaranteed that the variable "count" will assume each value between no1 and no2, inclusive. So you want to add all of these up.

In particular, a + b + c = (a + b) + c. Also, a + b + c = 0 + a + b + c. You'll also want a variable (perhaps named "sum"?) to hold the answer.

Let us know if you need more than that. I don't want to just write it for you, but you're very close.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.