dexter dexter - 2 months ago 9
PHP Question

how to determine the sequence number of odd /even numbers using php

how to determine the sequence number of odd /even numbers using php ?

example ,i want output like here

odd numbers (1,3,5,7,9)

output

1 = 1
3 = 2
5 = 3
7 = 4
9 = 5


even numbers (2,4,6,8,10)

output

2=1
4=2
6=3
8=4
10=5


how code to make this function in php?

edit/update
if input 1 then output = 1 , if input 3 then output = 2, if input 21 then output= 11, etc,,

Answer

Try out this php code

    <?php
    $result = '';
    if(isset($_POST['value'])){
        $num = $_POST['value'];
        $count = 0;
        $countOdd = 0;

    for($i=1; $i <= $num;  $i++){
        //for even numbers
        if($i % 2 == 0){
            $count++;
            if($i == $num){
                $result =  "The Number ".$num." is Even on ".$count;
            }
        }else {
            $countOdd++;
            if($i == $num){
            $result = "The Number ".$num." is Odd on ".$countOdd;
            }
        }
    }
 }
?>

HTML COde

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input type="text" name="value"/>
<input type="submit" value="Check"/><br/>
<?php echo $result;?>
</form>

It is working correctly :P