# Thread: Need help with while loop

1. ## Need help with while loop

I'm just starting out so bare with me

I'm in the middle of trying to create a program using while loop that has the user input two integers then displays the odd numbers between those numbers and again with the even numbers. I know I probably have to use a counter but how do I go about starting it. BTW there is more to the program but this is the part I'm struggling with. Thanks for any help you can give.

2. Welcome to the boards!

I'd suggest maybe posting some code you've already made so far, but from what it sounds like you're trying to do, you will want to look in to the modulus operator. This operator returns the remainder of a division operation.

So, for example, if you were to do:

10%2

the result would be 0, as 10/2 equals 5 without any remainder.

Now, however, if you did:

11%2

you'd get 1, because 11/2 equals 5.5, and 0.5*2=1 which is the result.

So, using this logic, you can then figure out if you're on an odd or even number, and using that knowledge, use a counter and increment by two (+=2) until you're at the end variable.

-Hope that helps

3. OK I'm not even close and his is not how I wanted to spend my bday lol The actual project is write a program that uses while loops to prefoem the following steps:
*prompt user to imput two integers (firstnum smaller than second)
*output all odd number between the two numbers
*output the sum of the even numbers between the two numbers
*output the numbers and their square between 1 and 10
output all uppercase letter (IE A B C D ...)
Here's what I have so far. Like I said I said I'm just starting out, actually just been about two weeks so far.

(I was just trying to work out the odd number part so far)

Code:
```#include <iostream>
using namespace std;

int main()
{
int num1, num2;
int odds;
int evens;

cout << "Enter two integers: " << "First number must be smaller than the second number";
cin >> num1, num2;
cout << endl;

if (num1 % 2 == 0)
num1 = odds;

else if (num1 % 2 != 0)
num1 = evens;

while(odds > num1 && odds < num2)
{

cout << odds << endl;
odds += 2;
}

cout << odds << endl;
cout << "did you get it right?" << endl;

return 0;
}```
OK I know I'm really off but just how far off am I?

I think I will not choose an online class for programming next time, gotta hear the teacher explain this..

4. try doing this:

Code:
```//for your user input, this makes it more reliable

cout << "Please enter an integer: ";
cin >> num1;
cout << endl << "Please enter another integer: ";
cin >> num2;
cout << endl;

//your if statement is fine, but you also need to find the higher number, otherwise your while loop won't work right

int upper;
if (num1 > num2)
upper = num1;
else
upper = num2;

while (odds <= upper)
{
cout << upper << endl;
odds += 2;
}```

5. First, let me point-out one thing, and a hint...

num1 = odds;

The equal sign assigns the value on the right to the variable on the left. It looks like equation, but it's not. It's an assignment statement. This won't work for you, 'cause odds doesn't have a value.

And here's the big hint:

Develop your code a few lines at a time. Put in some extra (temporary) cout statements to check your progress.

First, write the code to input the two numbers. Maybe add a cout statement to display the variable names and their values.

Compile and Test-run

Add an if-statement that displays "odd" if the first number is odd.

Compile and Test-run

When that works, add the "else" logic to display "even" if the first number is even.

Compile and Test-run

Now, make a loop that displays all of the numbers.

Compile and Test-run

Put your (working) odd-even logic into the loop... Display all of the numbers, and display the word "odd" or "even" after each one.

Compile and Test-run

Revise the if-logic so instead or displaying "odd" or "even" after each number, it displays just the odd numbers.

Compile and Test-run

Copy the loop and revise it so it displays the even numbers.

Compile and Test-run,

Add the code to sum the even numbers, etc., etc., etc...

Of course, you don't have to follow these exact steps, but you get the idea. As you gain experience and confidance, you can compile and test less frequently, but even expert programmers test their code as they develop it. Nobody writes a whole program before compiling & running.

I'm not suggesting that you design your programs a few lines at a time... you should have at-least an overall plan before you start... just write the actual code a few lines at a time.

You probably haven't studied functions yet, but using this same approach when writing functions will save you lots of greif.