Write a JavaScript code to display even number from 1 to 100 using looping statement
Answers
<!DOCTYPE html>
<html>
<body>
<script language="javascript" type="text/javascript">
var i
for(i=1;i<=100;i++)
if(i%2==0)
document.write(i + " <br>")
</script>
</body>
</html>
Answer:
JavaScript códe to display even number between 1 to 100 using looping statement.
for (let a = 1; a < 100; a++) {
if (a % 2 == 0) {
console.log(a)
}
}
Explanation:
In the above example, we have declared a variable named a and initialized it with 1.
We've used a for loop that executes up to 100 times and for each iteration of a the if statement checks whether the number is even or not.
After printing each even number, the value if a gets increased by 1.
In order to check the number, we've divided the number by 2 if it does'nt leave any remainder, the number is even and the print statement prints that number.