# How to write a function that prints out odd number 1-10?

Follow

- ✓Follow publicly
- ✓Follow privately
- Unfollow

I need to create a function in php that will print out only odd numbers from 1-10

Anyone can help me with this?

Anyone can help me with this?

### Other Answers (4)

Rated Highest-
kirstie's got a pretty solid example but there are a few things awry with the code ...

firstly the $limit variable should be set to 10 instead of 0, probably just a type-o, no biggie except it would cause an infinte loop as no positive interger will ever be less than 0.

Next the variable to run the modulus operation on is listed as $i, not sure where this came from, again probably just a type but will keep the function from running properly as $i is likely interpreted as NULL or 0.

Lastly, there is no call for the function.

So, good code just a few type-o's, i've done similar myself so no biggie. :) But this revision of her code should run smoother and output what you're looking for.

<?php

function PrintOdd($limit='10'){

$c = 1;

while($c <= $limit){

if ($c % 2!=0){

echo $c . "

\n";

}

$c++;

}

}

PrintOdd();

?>

p.s. if you like this as your best answer, please vote for kirstie instead as is her code, i just cleaned it up a little ... thanks :)### Source(s):

3+ years web development experience

...

...

vote for kirstie :) -
hi

use this

function print_odd()

{

for ($i=0;$i<=10;$i++)

if ($i % 2!=0)

echo $i;

}

bye for now -
I'm not sure if this is valid in php or not, but in C++ you can use the operator '%' to return a true of false value when dividing a number based on whether it results in a integer or a float; thus, if the number divided results in a whole number it returns 0; if there is a remainder, it returns 1.

Honestly I am still learning code myself (and I don't know php), but I think the function would look something like this:

for (i =1; i <= 10; i++)

{

if ( i % 2 == 1)

{

print i;

}

}

That should repeat the 'for' loop through values 1 -> 10, and if 'i' divided by 2 results in a float (0.5, 1.5, 2.5, etc) then the result of i%2 will be 1, thus printing i as a confirmed odd number. If it results in 0, it will repeat the process with the next increment of value i.

*edit*

I just tried this in C++ and it worked a charm, except that all the numbers were printed one after the other so it ended up with '13579'; that was easily fixed.

This is the C++ code:

for (int i =1; i <= 10; i++)

{

if ( i % 2 == 1)

{

std::cout << i << std::endl;

// std::cout tells it to print what follows '<<', and std::endl tells it to start a new line.

}

} -
I'm not sure how to do it in PHP, but in C#, there is a 'modulus' operator that returns only the remainder of integer division. So, basically, you create a loop, and try to divide the numbers 2 thru 10 by 2, and check to see if there is a remainder. If there is, it's an odd number - if not, it's even. The modulus operator in C# is the percent sign (%). You have to create a loop within a loop : the outside loop will count from 2 to 10 (you assume that it is known that 1 is an odd number), and the inside loop will count from 2 to (the number the outside loop is at). Give this a try, and if you still need help, just post more details, ok? Good luck!

Sign In

to add your answer