JavaScript

How to determine if a number is odd or even in JavaScript

In this tutorial, we are going to see how to determine if a number is odd or even in JavaScript. Given an integer and we need to check whether it is odd or even using Javascript.

An even number is an integer correctly divisible by 2. Example: 0, 4, 8, etc.

An odd number is an integer that is not correctly divisible by 2. Example: 1, 3, 7, 15, etc.

To check whether the given number is even or odd, we check the remainder of the division by dividing the number by 2.
 
[st_adsense]  

Program to check if a number is odd or even in JavaScript
<!doctype html>
<html>
   <head>
      <script>
         function check(){
              var nbr;
              nbr = Number(document.getElementById("myInput").value);
              if(nbr%2 == 0)
              {
                     alert("Even number");
              }
              else
              {
                     alert("Odd number");
              }
         }
      </script>
   </head>
   <body>
      Enter a number : <input id="myInput">
      <button onclick="check()">Check</button>
   </body>
</html>
Result
Enter a number :
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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