Java Program to Print Even Numbers From 1 to N

You are currently viewing Java Program to Print Even Numbers From 1 to N

Java program to print EVEN numbers from 1 to N

//Java program to print EVEN numbers from 1 to N.
 
import java.util.*;
 
public class Even{
 
    public static void main(String []args)
    {
        int n=0,i=0;
         
        Scanner X = new Scanner(System.in);
         
        System.out.print("Enter value n : ");
        n = X.nextInt();
         
        for(i=1; i<n; i++)
        {
            if(i%2==0)
                System.out.print(i+" ");
        }   

        System.out.println();
         
    }
}

Output

    
    [email protected]:~$ javac Even.java
    [email protected]:~$ java Even

    Enter value n : 10
    2 4 6 8 10

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply