Menu driven program using switch case in Java
switch case - Question 5
In this question, we will see how to create a menu-driven program in Java using the switch case statement. To know more about switch case statement click on the switch case statement lesson.
Q5) Write a menu-driven program using the switch case statement in Java to check whether a number is:
- Even number or Odd number
- Two digit positive number or not
- Multiple of 5 or not
Program
import java.util.Scanner;
public class Q5
{
public static void main(String args[])
{
int c,n;
Scanner sc=new Scanner(System.in);
System.out.println("1 Even number or Odd number");
System.out.println("2 Two digit positive number or not");
System.out.println("3 Multiple of 5 or not");
System.out.print("Enter your choice: ");
c=sc.nextInt();
switch(c)
{
case 1:
System.out.print("Enter a number ");
n=sc.nextInt();
if(n%2==0)
{
System.out.print("Even Number");
}
else
{
System.out.print("Odd Number");
}
break;
case 2:
System.out.print("Enter a number ");
n=sc.nextInt();
if(n>=10 && n<100)
{
System.out.print("Two digit positive number");
}
else
{
System.out.print("Not two digit positive number");
}
break;
case 3:
System.out.print("Enter a number ");
n=sc.nextInt();
if(n%5==0)
{
System.out.print("Multiple of 5");
}
else
{
System.out.print("Not multiple of 5");
}
break;
default:
System.out.print("Invalid Choice");
}
}
}
Output
1 Even number or Odd number 2 Two digit positive number or not 3 Multiple of 5 or not Enter your choice: 2 Enter a number 58 Two digit positive number