×

Java Programs

Java Practice

Java program to print EVEN numbers from 1 to N

Print Even Numbers from 1 to N in Java - In this program we will read value of N and print Even numbers from 1 to N.

Print Even Numbers from 1 to N using Java Program

//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

    
    me@linux:~$ javac Even.java
    me@linux:~$ java Even

    Enter value n : 10
    2 4 6 8 10

Core Java Example Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.