Even Odd program in JavaScript

In this program, the user will take input through the window prompt and find even or odd numbers in javascript.

<!DOCTYPE html>
<html>
    <body>
        <script>
         function EvenOdd()
         {
             var a=prompt("enter a number:");
	     if(a%2==0)
               document.write(a+" is Even number");
             else
	       document.write(a+" is odd number");
	  }
        </script>
        <form>
            <input type="button" value="EvenOdd" onclick="EvenOdd();">
        </form>
    </body>
</html>
Run now

Input has taken through javascript prompt method

output

enter a number:12
12 is Even number

Logic to check wether number is even or not

  • In this program, when the user clicks on the 'EvenOdd' button, EvenOdd() function will get called.
  • In the EvenOdd(), you will take input from a user by using the window prompt method and you will store user value in a variable a.
  • After taking a number from the user, now check whether the number is completely divisible by 2 or not by using the if statement. if the number is divisible by 2 then it will give a reminder of 0.
  • If the if-statement is true then the number is even and you will print "number is even"
  • if the if-statement is not true then the else part will get executed and you will print, the number is odd.