Even Odd Programms in PHP

Even numbers are those which are divisible by 2. Numbers like 2,4,6,8,10, etc are even.

Odd numbers are those which are not divisible by 2. Numbers Like 1, 3, 5, 7, 9, 11, etc are odd.

Logic:

  • Take a number.
  • Divide it by 2.
  • If the remainder is 0, print number is even.

Even Odd Program in PHP

A program to check 1233456 is odd or even is shown.

Example:

  1. <?php
  2. $number=1233456;
  3. if($number%2==0)
  4. {
  5.  echo “$number is Even Number”;
  6. }
  7. else
  8. {
  9.  echo “$number is Odd Number”;
  10. }
  11. ?>

Output:

PHP Even odd program 1


Even Odd Program using Form in PHP

By inserting value in a form we can check that inserted value is even or odd.

Example:

  1. <html>
  2. <body>
  3. <form method=“post”>
  4.     Enter a number:
  5.     <input type=“number” name=“number”>
  6.     <input type=“submit” value=“Submit”>
  7. </form>
  8. </body>
  9. </html>
  10. <?php
  11.     if($_POST){
  12.         $number = $_POST[‘number’];
  13.         //divide entered number by 2 
  14.         //if the reminder is 0 then the number is even otherwise the number is odd
  15.         if(($number % 2) == 0){
  16.             echo “$number is an Even number”;
  17.         }else{
  18.             echo “$number is Odd number”;
  19.         }
  20.     }
  21. ?>

Output:

On entering the number 23456, following output appears.

On entering the number 285965, following output appears.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *