Tuesday, 22 November 2016

Menu driven Java program

In this program we have used switch case statement to implement user friendly program. The java.util.Scanner class is used to read input from keyboard at runtime. If no choice is match then the deafault statement will get executed.


PROGRAM
import java.util.Scanner;

public class MenuDrivenProg {

 public static void main(String[] args) {
  
  int choice, num;
  Scanner n = new Scanner(System.in);
  System.out.println("Menu....");
  System.out.println("1. Find positive");
  System.out.println("2. Odd/Even");
  System.out.println("Enter the choice : ");
  choice = n.nextInt();
 
  switch(choice) {
  
   case 1:
    System.out.println("Enter number : ");
    num = n.nextInt();
    if(num > 0)
     System.out.println("Positive...");
    else
     System.out.println("Negative...");
    break;
   case 2:
    System.out.println("Enter number : ");
    num = n.nextInt();
    if(num%2 == 0)
     System.out.println("Even...");
    else
     System.out.println("Odd...");
    break;
   default:
    System.out.println("Wrong choice..");
  }
 }
}
OUTPUT 1
C:\>javac MenuDrivenProg.java
C:\>java MenuDrivenProg
Menu....
1. Find positive
2. Odd/Even
Enter the choice :
1
Enter number :
-20
Negative...
OUTPUT 2
C:\>javac MenuDrivenProg.java
C:\>java MenuDrivenProg
Menu....
1. Find positive
2. Odd/Even
Enter the choice :
2
Enter number :
63
Odd...

No comments:

Post a Comment