Write a Java-program to check Odd or Even number using switch

Write a Java-program to check Odd or Even number using switch

Program
public class Main
{
	public static void main(String args[])
	{
        int x=10,y;
        y=x%2;
        switch(y)
        {
        case 0:
             System.out.println("Even Number");
             break;
        case 1:
             System.out.println("Odd Number");
             break;
        default:
             System.out.println("Invalid Choice");
        }
	}
}	

Output:

Even Number

Using CommandLine Arguments
public class Main
{
	public static void main(String args[])
	{
        int x,y;
        x=Integer.parseInt(args[0]);
        y=x%2;
        switch(y)
        {
        case 0:
             System.out.println("Even Number");
             break;
        case 1:
             System.out.println("Odd Number");
             break;
        default:
             System.out.println("Invalid Choice");
        }
	}
}	

Output:

Even Number

Using Scanner Class
import java.util.*;
public class Main
{
	public static void main(String args[])
	{
        int x,y;
        System.out.println("Enter x value :");
        Scanner s=new Scanner(System.in);
        x=s.nextInt();
        y=x%2;
        switch(y)
        {
        case 0:
             System.out.println("Even Number");
             break;
        case 1:
             System.out.println("Odd Number");
             break;
        default:
             System.out.println("Invalid Choice");
        }
	}
}	

Output:

Enter x Value : 10
Even Number





More Questions


39 . Write a Java-program to check Odd or Even number program using nested if condition(value must be positive)
40 . Write a Java-program to check Odd or Even number using switch
41 . Write a Java-program to check Vowel or Consonant using switch
42 . Write a Java-program to display 1 to 10 values using for loop
43 . Write a Java-program to display 10 to 1 values using for loop
44 . Write a Java-program to display 1 to n values using for loop
45 . Write a Java-program to display display Even Numbers using for loop
46 . Write a Java-program to display Even Number or Odd Number based on the N value using for loop
47 . Write a Java-program to display Even Number or Odd Number based on the N value using single for loop
48 . Write a Java-program count the number of divisible numbers in a given range(1-100) ?
49 . Write a Java-program count the number of divisible numbers in a given range(1-100) without loop