Header Ads

How do you print even numbers in JavaScript?


If the number gives 0 remainder when divided by 2, then the number is even.
We will use for loop to print the even numbers between 0 - 10.

JavaScript    Java

Code

for(i=0; i<=10; i++){
    if(i%2 == 0){
        console.log(i)
    }
}

Output :

 0
 2
 4
 6
 8
 10

Explanation :

In this problem we have to iterate no. through for loop and check weather the number  is even or not. If the number is even then it is printed using the statement ' console.log(i) ' else the loop jumps to next execution.

Post a Comment

0 Comments