Q:

Java program to print EVEN numbers from 1 to N

0

Java program to print EVEN numbers from 1 to N

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now