var a=prompt("enter a number:");
document.write(a+" is Even number");
document.write(a+" is odd number");
<input type="button" value="EvenOdd" onclick="EvenOdd();">
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() 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
- 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.