Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

  1. Home >
  2. All Categories >
  3. Computers & Internet >
  4. Programming & Design >
  5. Resolved Question
Kitty Kat Kitty Kat
Member since:
November 30, 2006
Total points:
149 (Level 1)

Resolved Question

Show me another »

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

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

Anyone can help me with this?
kirstie s by kirstie s
Member since:
September 04, 2007
Total points:
170 (Level 1)

Best Answer - Chosen by Voters

you could use the old for loop which is not really best practise these days.... however the way I would do it is:

<?php

function PrintOdd($limit='0'){

$c = 1;
while($c <= $limit){
if ($i % 2!=0){
echo $c;
}
$c++;
}

}


?>

Source(s):

my brain
50% 2 Votes
  • 1 person rated this as good

There are currently no comments for this question.

Other Answers (4)

  • has_infoooo by has_info...
    Member since:
    November 04, 2006
    Total points:
    2,030 (Level 3)
    hi
    use this
    function print_odd()
    {
    for ($i=0;$i<=10;$i++)
    if ($i % 2!=0)
    echo $i;
    }
    bye for now
    25% 1 Vote
  • Jonathan C by Jonathan C
    Member since:
    September 04, 2007
    Total points:
    214 (Level 1)
    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.
    }
    }
    0% 0 Votes
  • superSymmetric by superSym...
    Member since:
    July 19, 2007
    Total points:
    14,142 (Level 6)
    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!
    0% 0 Votes
  • hagakure by hagakure
    Member since:
    October 21, 2006
    Total points:
    797 (Level 2)
    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 :)
    0% 0 Votes

Answers International

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer.

Help us improve Yahoo! Answers. Send Feedback