Javascript odd or even numbers
Learn how to print odd or even numbers in JavaScript.
Example:
Printing Even numbers:
document.write("Even Numbers:<br>");
for(var i=1;i<=10;i++){
if(i%2==0){
document.write(i, ", ");
} }
Printing odd numbers:
document.write("<br> Odd Numbers: <br>");
for(var i=1;i<=10;i++){
if(i%2!=0){
document.write(i, ", ");
}
}
To print numbers, we must use “for loop”. Here i=1, we set condition less than or equal to 10 “i<=10“. So, the loop is run less than 10.
To print even numbers, we need to check the condition the given number is “i%2==0” using if condition statement as in the above code.
To print odd numbers, we need to check the condition the given number is “i%2!=0” using if condition statement as in the above code.