JavaScript Odd or Even Program
In this tutorial, we will learn how to create a simple JavaScript program to check if a number is odd or even.
JavaScript Odd or Even Program
Here’s a simple JavaScript program that takes a number as input and checks if it’s odd or even:
“`javascript
function checkOddOrEven(num) {
if (num % 2 === 0) {
return “Even”;
} else {
return “Odd”;
}
}
var number = prompt(“Enter a number:”);
var result = checkOddOrEven(number);
alert(“The number is ” + result);
“`
This program defines a function checkOddOrEven
that takes a number as input and uses the modulus operator to check if it’s even or odd. If the remainder is 0, the number is even; otherwise, it’s odd.
Testing the Program
To test the program, you can create an HTML file with the above JavaScript code and open it in a web browser. You’ll be prompted to enter a number, and the program will display whether it’s odd or even.
Conclusion
Creating a JavaScript program to check if a number is odd or even is a simple yet useful exercise for beginners. It demonstrates the use of basic JavaScript syntax and operators to perform a common task.