Even and Odd Number Using For Loop
Even number means when you divide that number by 2 and remainder is zero.
e.g.4%2 remainder is 0
var i=4;
if(4%2==0)
document.write("Even Number");
Odd number means when you divide that number by 2 and remainder is not equal to zero.
e.g.5%2 remainder is not equal to zero.
var i=5;
if(5%2!=0)
document.write("Odd Number");
Example-Accept number from user and check whether it is odd or even.
<!DOCTYPE html>
<html>
<head>
<title>Javascript Program</title>
</head>
<body>
<h1>Even Odd Number</h1>
<script type="text/javascript">
var i,n;
n=prompt("Enter Any Number To Check");
if(n%2==0)
{
document.write(n+"Is Even Number");
}
else
{
document.write(n+"Is Odd Number");
}
</script>
</body></html>
2.Even numbers from 1 to 10.
<!DOCTYPE html>
<html>
<head>
<title>Javascript Program</title>
</head>
<body>
<h1>Even Number</h1>
<script type="text/javascript">
var i;
for(i=1;i<=10;i++)
{
if(i%2==0)
{
document.write(i+" "+"Is Even Number"+"<br>");
}
}
</script>
</body></html>
Output-
Even Number
2 Is Even Number
4 Is Even Number
6 Is Even Number
8 Is Even Number
10 Is Even Number
3.Odd Numbers from 1 to 10.
<!DOCTYPE html>
<html>
<head>
<title>Javascript Program</title>
</head>
<body>
<h1>Odd Number</h1>
<script type="text/javascript">
var i;
for(i=1;i<=10;i++)
{
if(i%2!=0)
{
document.write(i+" "+"Is Odd Number"+"<br>");
}
}
</script>
</body></html>
Output-
Odd Number
1 Is Odd Number
3 Is Odd Number
5 Is Odd Number
7 Is Odd Number
9 Is Odd Number
Please Comment and Share. ConversionConversion EmoticonEmoticon