# Thread: problem with while loops

1. ## problem with while loops

I'm learning while loops and I'm having trouble with this program I'm writing. The user inputs 2 numbers and I have to be able to display different things, like the odd numbers between the numbers entered. This is what I have so far. If someone could steer me in the right direction, I'd really appreciate it.

Code:
```#include <iostream>

using namespace std;

int main()
{
int firstNum;
int secondNum;

cout << "Enter two numbers." << endl;
cout << "First number must be less than or equal "
<< "to the second number you enter" << endl;
cout << "Enter numbers: ";
cin >> firstNum >> secondNum;
cout << endl;

while (firstNum <= secondNum)
{
cout << "Odd integers between " << firstNum << " and "
<< secondNum << "are: ";```

2. For starters, do you know how to test if an integer is odd/even?

3. the easiest way to find if a number is odd is to use the module %
what it does is it devides and returns the remainder for example:
10%3
would return 1
10%2
would return 0

so if a number is divisable by 2 and no remainder is left than its even.. and if has a remainder than its odd

4. nope the easiest way is odd=number_to_test & 1;
Miles quicker than modulo

5. Originally Posted by Stoned_Coder
nope the easiest way is odd=number_to_test & 1;
Miles quicker than modulo
I'd say that's the fastest not the easiest since it relies on bitwise operators and binary number representation which might not be that obvious to a beginner

6. Ok, so I've tried to incorporate what you all have said. Let me know if this looks alright to you all!

Code:
```#include <iostream>

using namespace std;

int main()
{
int firstNum;
int secondNum;
int sum;
int squareSum;

cout << "Enter two numbers." << endl;
cout << "First number must be less than or equal "
<< "to the second number you enter" << endl;
cout << "Enter numbers: ";
cin >> firstNum >> secondNum;
cout << endl;

while (firstNum <= secondNum)
{
cout << "Odd integers between " << firstNum << " and "
<< secondNum << "are: \n";
if (firstNum % 2 == 0)
{
firstNum = (2 * firstNum) - 1;
cout << firstNum << " " << endl;
}
else if (firstNum % 2 == 1)
{
firstNum = firstNum + 2;
cout << firstNum << " " << endl;
}```

7. like stoned coder said you should do this:
Code:
```int number;
cin >> number;
if (number & 1) cout << "ODD";
else cout << "EVEN";```
What it does is bit operates on the smallest bit of number.

1 & 1 = 1
0 & 1 = 0

etc...

Since the smallest bit has a value of 1 which is 2 to power of 0 so its the only bit that could make a number odd.

8. Although it may be a little advanced, you should check out bit operations. But if its because your trying to get a hang of modulus operator then go ahead.

also what is this for:
Code:
`firstNum = (2 * firstNum) - 1;`
why not just do this
Code:
`firstNum++;`
It will just up firstNum to make it odd

9. Originally Posted by Stoned_Coder
nope the easiest way is odd=number_to_test & 1;
Miles quicker than modulo
You are heavily underestimiting the intelligence of compiler technology.

"Every" compiler will optimize (a % 2) into (a & 1).

10. Originally Posted by JoshR
Although it may be a little advanced, you should check out bit operations. But if its because your trying to get a hang of modulus operator then go ahead.

also what is this for:
Code:
`firstNum = (2 * firstNum) - 1;`
why not just do this
Code:
`firstNum++;`
It will just up firstNum to make it odd

I'm not trying to increment "firstNum" by one every time... I'm trying to display the odd numbers between whatever two numbers the user inputs. But does everything look ok as far as my set up? If I try to do something more with "firstNum" after what I've done already, how do I change it back to what the user input for "firstNum"? Cuz through the "while" and "if" stuff, didn't I change it? I need to be able to use the original "firstNum" again in the program. Suggestions?

11. >> I'm not trying to increment "firstNum" by one every time
You should be. You should go through each number between firstNum and secondNum, one number at a time in the while loop. To do this, increment firstNum by one every time through the loop.

>> I need to be able to use the original "firstNum" again in the program.
Then create a new variable with a different name, like counter or something. Before your loop, assign it to the value of firstNum. The increment it each time and leave firstNum alone.

12. So would I sort through all the numbers between the first and second number into odd and even in an "if...else" or what?

13. Just change firstNum line to firstNum++; and it will work, see if you can figure out the logic...

14. >Miles quicker than modulo
Modulus is obvious though. The same can't be said for AND. Then, of course, there's the argument that such micro-optimizations are silly until you know that they'll make a difference. And in the end, a wise choice of algorithm will usually make the point moot.

15. Ok, this is my "finished" code for what I've been trying to do. But I don't know why my loops are infinite when I execute the program so I can't check to see if the rest of my program runs the way I want it to. Hopefully you all can check my code out and let me know what I'm doing wrong or missing. Thanks.

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

int main()
{
int firstNum;
int secondNum;
int sum;
int squareSum;
int counter;
int i;
int square;

cout << "Enter two numbers." << endl;
cout << "First number must be less than or equal "
<< "to the second number you enter" << endl;
cout << "Enter numbers: ";
cin >> firstNum >> secondNum;
cout << endl;

sum = 0;
counter = firstNum;
while (counter <= secondNum)
{
cout << "Odd integers between " << firstNum << " and "
<< secondNum << " are: \n";
counter = (2 * firstNum) - 1;
counter++;
cout << counter << " " << endl;

cout << "Sum of even integers between " << firstNum << " and "
<< secondNum << " = ";

counter = firstNum;
sum = sum + counter;
counter = (2 * firstNum);
counter++;
cout << sum << " " << endl;
}

cout << left << setw(30) << "Number"
<< setfill(' ') << right << "Square of Number" << endl;
while (i <= 10)
{
square = i * i;
cout << setw(30) << i << setfill(' ') << right << square << endl;
i++;
}

squareSum = 0;
counter = firstNum;
cout << "Sum of the squares of odd integers between "
<< firstNum << " and " << secondNum << " = ";
while (counter <= secondNum)
{
counter = (2 * firstNum) - 1;
squareSum = squareSum + (counter * counter);
counter++;
cout << squareSum << endl;
}

while ('A' <= 'Z')
{
cout << "Upper case letters are: "
<< 'A' << " " << endl;
'A++';
}

return 0;
}```

Popular pages Recent additions