Odd Even Program in Javascript

Programming is an essential skill in the digital age, and understanding fundamental concepts like conditional statements and loops is crucial for any budding developer. One of the simplest yet powerful exercises to grasp these concepts is by writing an odd-even program. In this blog, we will guide you through creating an odd-even program in JavaScript, covering the basics and providing a step-by-step approach to help you understand and implement the solution.

What is an Odd Even Program?

An odd-even program is a simple script that determines whether a given number is odd or even. An even number is divisible by 2 without any remainder, while an odd number leaves a remainder of 1 when divided by 2. This type of program is an excellent exercise for practicing conditional logic in programming.

Setting Up Your Environment

Before we dive into coding, ensure you have a suitable environment for writing and running JavaScript code. You can use a text editor like Visual Studio Code, Sublime Text, or any other editor you prefer. Additionally, you’ll need a web browser (like Chrome, Firefox, or Edge) to run your JavaScript code.

Code of Odd Even Program in Javascript

This HTML document contains a simple JavaScript program to check if a number is odd or even. 

<!doctype html>
<html>
<head>
<script>
var num, temp;
function fun()
{
  num = parseInt(document.getElementById("num").value);
  if(num)
  {
	temp = document.getElementById("resPara");
	temp.style.display = "block";
	if(num%2==0)
  	document.getElementById("res").innerHTML = "Even";
	else
  	document.getElementById("res").innerHTML = "Odd";
  }
}
</script>
</head>
<body>
<h1>Odd even program in javascript</h1>
<p>Enter the Number: <input id="num"></p><button onclick="fun()">Check</button>
<p id="resPara" style="display:none;">It is an <span id="res"></span> Number</p>

</body>
</html>

Explanation of the Code:

  • The script defines a function `fun()` that is triggered when the “Check” button is clicked. 
  • The function reads the input value from the text field with the id `num`, converts it to an integer using `parseInt()`, and checks if it’s a valid number. 
  • If it is, it displays a paragraph (`resPara`) and determines if the number is even or odd using the modulus operator (`%`). 
  • The result is then displayed in the `res` span within the paragraph. If the number is even, “Even” is displayed; otherwise, “Odd” is shown. 
  • The paragraph remains hidden (`display:none`) until a valid number is entered.

Output:

Real Life Application of Odd Even Program

  1. Traffic Management: In cities with high traffic congestion, odd-even vehicle rationing schemes are implemented. JavaScript can be used in web applications or mobile apps to check vehicle plate numbers and determine whether a vehicle is allowed on the road on a particular day based on the odd or even rule.
  2. Game Development: In games, odd-even logic can determine various in-game events or character actions. For instance, certain rewards or penalties might be triggered based on whether a player’s score is odd or even.
  3. Event Scheduling: For event management systems, odd-even logic can help in scheduling tasks, assigning rooms, or managing resources. For example, odd-numbered days might be allocated to one set of activities, while even-numbered days are for another.
  4. Inventory Management: In inventory systems, odd-even checks can be used to categorize products or manage stock rotation. For instance, products with odd-numbered IDs could be checked for restocking on specific days of the week.
  5. Utility Billing: Utility companies could use odd-even logic for meter reading schedules, ensuring an even distribution of workload by alternating readings between odd and even-numbered addresses.
  6. User Interface Customization: Web applications can use odd-even logic to apply different styles or functionalities to elements. For example, alternate rows in a table can be styled differently to enhance readability or user inputs can trigger different responses based on odd or even values.

Congratulations! You’ve just learned a basic odd-even program in JavaScript. This blog introduces you to essential programming concepts like conditional statements and DOM manipulation. As you continue to learn and practice with Newtum, you’ll find these foundational skills invaluable in building more complex applications.

By experimenting with this simple program, you’re taking your first steps toward mastering JavaScript and understanding how logic and interactivity work in web development. Keep practicing and exploring new challenges to enhance your coding skills further. Happy coding!

About The Author

Leave a Reply