Write a Java-program to display Even Number or Odd Number based on the N value using while loop ?

Write a Java-program to display Even Number or Odd Number based on the N value using while loop ?

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

Output:

2
4
6
8
10

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

Output:

2
4
6
8
10

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

Output:

Enter n value : 10
2
4
6
8
10





More Questions


61 . Write a Java-program to display display Even Numbers using while loop
62 . Write a Java-program to display Even Number or Odd Number based on the N value using while loop
63 . Write a Java-program to display Even Number or Odd Number based on the N value using single while loop
64 . Write a Java-program count the number of divisible numbers in a given range(1-100) using while loop ?
65 . Write a Java-program to print Factorial Number using while loop ?
66 . Write a Java-program to print Factorial of upto N Numbers using while loop ?
67 . Write a Java-program to print sum of natural numbers using while loop ?
68 . Write a Java-program to print sum of Even numbers using while loop ?
69 . Write a Java-program to print sum of Odd numbers using while loop ?
70 . Write a Java-program to print sum of even or odd numbers based on the N value using while loop ?
71 . Write a Java-program to print sum of even or odd numbers based on the N value using single while loop ?