What is Odd Even ?

Odd Number : Any integer that cannot be divided exactly by 2 is an odd number
Odd Number : 1 3 5 7 9 11
Even Numbers : Any integer that can be divided exactly by 2 is an even number.
Even Numbers : 0 2 4 6 8 10

Function

function check(){
            var number = 0;
            number = document.getElementById("valuenumber").value;
            var answer = (number % 2 == 0) ? " Even " : " Odd ";
            alert(number +" is "+ answer);
         }

Program

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Odd Even </title>
		</head>
		<body>		
		<div>
				<h3> Check Number is Odd Or Even </h3>
				<form method="post">
					<p>
						<strong>
							Enter The Number :
						</strong><input type="number" id="valuenumber" onfocusout="checkoddeven()" autofocus="" maxlength="4"  min="1" max="1000" required  /></p>
						<p> <button onclick="checkoddeven()">Check Number</button></p>
				</form>
				<div id="result" class="result"></div>
		</div>

		<script type="text/javascript">
      	function checkoddeven() {
      		var number = 0;
      		number = Number(document.getElementById("valuenumber").value);
      		try {
      			if (number == 0 || number == null) {
      				alert("Please Enter Value");
      			} else {
      				var result = "";
      				result += "<p class=\"longword\"> "+number;
      				result += (number % 2 == 0) ? " Even " : " Odd ";
      				result += "</p>";
      				document.getElementById("result").innerHTML = result;
      			}
      		} catch(err) {
      		}
      	} 		
 		</script>
	</body>
</html>

Explanation

Step 1. First make form with define with id. Like valuenumber.
Step 2. Button for trigger the function which check the values by using onclick=”checkoddeven()”
Step 3. Make function name of checkoddeven()
Step 4. Declare variable number; Like var number; 
Step 5. document.getElementById(“valuenumber”).value; is for get value by id.
Step 6. Number() convert any value to number.If any text its tell zero.
Step 7. Try Catch its a function which first try until not error generated,if any error generated other function Catch then Error you print cause of error.
step 8. Validation by if number value is zero or Null then alert(“Please Enter Value”)
Step 9. (number % 2 == 0) ? ” Even ” : ” Odd “; is Short version of If Else. which check the Number is Odd or Even. Any Even Number is Divide Completely with 2, and then if remainder is equal to zero its means it is even number,Else its odd number.
Step 10. document.getElementById(“result”).innerHTML is for display the result on div after completed.
Step 11. innerHTML is property which add text between Tags.LIKE  <div id=”result”>  Text </div>  or <p id=”result”>Text</p>.

Demo

(Visited 623 times, 1 visits today)
Share with Friends :
Written by:

Leave a Reply

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