write a program to print odd numbers series using do while statement

Dani AI

Generated

A quick, robust alternative to the short demo from is a reusable function that handles arbitrary start/end values, reversed bounds and the case where the start is even. It uses a do/while as requested but avoids printing numbers outside the requested inclusive range.

<?php
function print_odds_in_range($start, $end) {
    $start = (int)$start;
    $end   = (int)$end;

    if ($start > $end) {
        list($start, $end) = array($end, $start); // allow reversed inputs
    }

    if ($start % 2 === 0) {
        $start++; // move to the first odd inside the range
    }

    if ($start > $end) {
        return; // nothing to print (e.g., start and end were the same even number)
    }

    $n = $start;
    do {
        echo $n . "<br />\n";
        $n += 2;
    } while ($n <= $end);
}

Notes: inputs are cast to integers to avoid PHP warnings; the function returns early when the adjusted first odd exceeds the end so the do/while does not produce an out-of-range value. For CLI output replace the HTML line break with PHP_EOL. Incrementing by 2 is more efficient than testing every number with % 2. As suggested, posting a minimal reproducible snippet and expected output helps when troubleshooting. The short example and the external reference mentioned earlier are fine for learning the statement, but the function above is safer for reuse in real code.

Are you thinking that someone will write that code for you? Guess again.

What have you got so far. Post your code.

Printing odd numbers? Shouldn't really be that hard.

$num = 1;
do {
    echo $num.'<br />';
    $num = $num+2;
} while ($num < 100);

Quite easy, just change the 100 to how far you want it to go.

Without putting some self effort we can't help you, and from this question, it looks like you are not serious in learning how to code.

Member Avatar for Member #120589

Looks like somebody didn't take the hint :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.