In this code, we are going to find the odd and even number from 1 to 100 in Javascript. To understand this you need to know about the for loop and if-else statement.
We will divide the code into three programs.
The logic of the code is:
If the number % 2 == 0 then it is an even number else it is an odd number.
How to find Odd numbers from 1 to 100 in Javascript.
for(let i = 1; i <= 100; i++ )
{
if(i % 2 != 0)
{
console.log(i)
}
}
The output of the code is
1 3 5 7... upto 99
How to find Even numbers from 1 to 100 in Javascript.
for(let i = 1; i <= 100; i++ )
{
if(i % 2 == 0)
{
console.log(i)
}
}
The output of the code is
2,4,6,8.... upto 96,98, 100
How to find both odd and even numbers.
for(let i = 1; i <= 100; i++ )
{
if(i % 2 == 0)
{
console.log(i+" is Even number")
}
else
{
console.log(i+" is Odd number")
}
}
The output of the code is
"1 is Odd number"
"2 is Even number"
"3 is Odd number"
"4 is Even number"
"5 is Odd number"
"6 is Even number"
.
.
"96 is Even number"
"97 is Odd number"
"98 is Even number"
"99 is Odd number"
"100 is Even number"