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;

}

Recommended Answers

All 2 Replies

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.

thanks for the help it works.

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.