Even or Odd Number Program
In this tutorial, you are given a number. Write a JavaScript program with a function that takes the number as argument, and prints if the given number is even or odd.
Solution
An even number leaves a remainder of 0 when divided by 2, and the odd number leaves a reminder of 1 when divided by 2.
We can use arithmetic modulus operator to find the remainder of the division of given number by 2.
Programs
- In the following program, we shall check if value in
num
is even or odd number using if-else statement.
var num = 10;
if (num % 2 == 0) {
console.log('even number');
} else {
console.log('odd number');
}