While statement displaying odd numbers

Thank you in advance to whoever can help me.

I’m not completely hopeless with PHP, but the book we are using for class is difficult in that the examples given in the book do not really assist in explaining the homework. That being said,

I am trying to write a while statement that displays all the odd numbers between 1 and 100. This is what I’ve got so far,

[php]<?php
$Count = 1;
while ($Count < 100) {
$Count = $Count % 2 != 0;
echo “$Count”;
}
?>[/php]

I guess I’m missing how to get the result to display more than just the number one… I’m sure I’m probably missing more than that.

Any help would be appreciated.

Our book didnt mention not being able to use an if statement - which is what I just did to complete the problem,

BUT is there a way to do this without an if statement?

this will display odd number from 1 - 101…
[php]
$Count = 1;
while ($Count < 100) {
$Count = $Count+2;
echo "$Count ";
}
[/php]

That code will actually start with 3 instead of 1. You actually need to put the echo statement before the math statement for it to include the 1.

ahh good point, didn’t even notice that, thank you :stuck_out_tongue:

[php]

$Count = 1;
while ($Count < 100) {
echo "$Count ";
$Count = $Count+2;
}
[/php]

there, thats better.

Sponsor our Newsletter | Privacy Policy | Terms of Service