简体   繁体   中英

How to get output for even odd numbers like this?

How to get output like this (range 25-50):

Odd number: 25 Even number: 26 Odd number: 27 Even number: 28... Even number: 50

Here is my code

<!DOCTYPE html>
<html>
<body>
<?php
for($i=2;$i<=25;$i+=2)
{
    echo "Odd number: $i <br>";

for($b=1;$b<=50;$b+=2)
{ 
    
    echo "Even number: $b <br>";
    
}
  
}
?>
</body>
</html>

You can just use the modulo operator to separate even and odd numbers.

<!DOCTYPE html>
<html>

<body>
    <?php
    for ($i = 25; $i <= 50; $i++) {
        if ($i % 2 == 1) {
            echo "Odd number: $i <br>";
        } else {
            echo "Even number: $i <br>";
        }
    }
    ?>
</body>

</html>

You can get it using loops. There can be various solutions. Here is one.

<!DOCTYPE html>
<html>
<body>
<?php
$i =25;
while($i<=50) {
  echo "Odd number: ". $i++ . "<br>";
  echo "Even number: ". $i++ . "<br>";
}
?>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM