Quote:Original post by Ultimatepuff
I have no idea where to start in putting it into my code.
Haven't followed the entire thread, but I assume this is about problem 2:
Quote:Output all odd numbers between firstNum and secondNum.
You already know how to find out whether a number is odd or even. There are more ways to do it, but the suggested manner should do fine.
We start the loop with the highest number. First, let's find out if this number is odd or even:
if (secondNum % 2 == 1) remainder = odd;else remainder = even; // we don't need to test again: if it's not odd, it has to be even.
Let's see some examples of what the output should look like in several cases:
firstNum = 10 secondNum = 21output: 11, 13, 15, 17, 19firstNum = 11secondNum = 28output: 13, 15, 17, 19, 21, 23, 25, 27firstNum = 7secondNum = 16output: 9, 11, 13, 15
As you may notice, we can start our counter at either
secondNum - 1 if the number is even, or
secondNum - 2 if the number is odd. Let's use that in our code:
if (secondNum % 2 == 1) counter = secondNum - 2; // odd;else counter = secondNum - 1; // even
Again, as you can see in the example outputs, the only thing left to do is to decrease this counter by 2 every iteration, until it is equal or smaller to
firstNum. There's no need to keep checking for the odd/even property, because if say, a number is odd then every time you add or substract a multiple of 2, you get a new number that is also odd. Same goes for even numers. The codez:
while (counter >= firstNum){ cout << counter << "\n"; counter -= 2;}