Write a Java-program to display display Even Numbers using do while loop

Write a Java-program to display display Even Numbers using do while loop

Program
public class Main
{
    public static void main(String args[])
    {
        int x=1,n=10;
        do
        {
            if(x%2==0)
            {
                System.out.println(x);
            }
            x++;
        }while(x<=n);
    }
}	

Output:

2
4
6
8
10

Using CommandLine Arguments
public class Main
{
    public static void main(String args[])
    {
        int x=1,n;
        n=Integer.parseInt(args[0]);
        do
        {
            if(x%2==0)
            {
                System.out.println(x);
            }
            x++;
        }while(x<=n);
        
    }
}	

Output:

2
4
6
8
10

Using Scanner Class
import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        int x=1,n;
        Scanner s=new Scanner(System.in);
        System.out.println("Enter n value : ");
        n=s.nextInt();
        do
        {
            if(x%2==0)
            {
                System.out.println(x);
            }
            x++;
        }while(x<=n);
    }
}	

Output:

Enter n value : 10
2
4
6
8
10





More Questions


77 . Write a Java-program to display display Even Numbers using do while loop
78 . Write a Java-program to display Even Number or Odd Number based on the N value using do while loop
79 . Write a Java-program to display Even Number or Odd Number based on the N value using single do while loop
80 . Write a Java-program count the number of divisible numbers in a given range(1-100) using do while loop ?
81 . Write a Java-program to print Factorial Number using do while loop ?
82 . Write a Java-program to print Factorial of upto N Numbers using do while loop ?
83 . Write a Java-program to print sum of natural numbers using do while loop ?
84 . Write a Java-program to print sum of Even numbers using do while loop ?
85 . Write a Java-program to print sum of Odd numbers using do while loop ?
86 . Write a Java-program to print sum of even or odd numbers based on the N value using do while loop ?
87 . Write a Java-program to print sum of even or odd numbers based on the N value using single do while loop ?