+ 5
To find the even number it is pretty basic, any number divisible by 2 is an even number for eg: 10 Ă· 2 = 5, 5 is the quotient, reminder is 0 so, if the reminder is 0 (zero) then it is an even number other it is an oddnumber. In PHP there are two operators (/) slash and (%) percentage, former one returns the quotient and the later one returns the reminder. In our program we need to find the reminder to check whether the entered number is odd or even, the basic script would be this: PHP 1 2 3 4 5 if(($number % 2) == 0){ echo "You entered an Even number"; }else{ echo "You entered an Odd number"; } You got an Idea how the script will work right? Here is the complete code for you: PHP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <html> <body> <h3>Odd or Even PHP Example</h3> <form action="" method="post"> <label>Enter a number</label><input type="text" name="number" /> <input type="submit" /> </form> </body> </html> <?php if($_POST){ $number = $_POST['number']; //check if the enter value is a number or throw an error if (!(is_numeric($number) && is_int(0+$number))){ echo "<p style='color:red;'>Error: You entered a string. Please enter an Integer</p>"; die(); } //divide the entered number by 2 //if the reminder is 0 then the number is even otherwise the number is odd if(($number % 2) == 0){ echo "You entered an Even number"; }else{ echo "You entered an Odd number"; } } ?> ↓↓↓ :) http://www.tutorialsmade.com/find-odd-or-even-number-in-php-with-example-script/
20th Dec 2017, 2:07 PM
James16
James16 - avatar