You are on page 1of 2

A program that lets you select an operator and then calculate two numbers based on what operator you

choose.

Source Code

import java.util.Scanner;
public class Menu_Driven
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int num1,num2,choice;

System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.print("Select your Choice(1-4) : ");
choice = scanner.nextInt();

switch(choice)
{
case 1:
System.out.print("Enter the Number 1 :");
num1=scanner.nextInt();
System.out.print("Enter the Number 2 :");
num2=scanner.nextInt();
System.out.print("Addition = " + (num1+num2));
break;
case 2:
System.out.print("Enter the Number 1 :");
num1=scanner.nextInt();
System.out.print("Enter the Number 2 :");
num2=scanner.nextInt();
System.out.print("Subtraction = " + (num1-num2));
break;
case 3:
System.out.print("Enter the Number 1 :");
num1=scanner.nextInt();
System.out.print("Enter the Number 2 :");
num2=scanner.nextInt();
System.out.print("Multiplication = " + (num1*num2));
break;
case 4:
System.out.print("Enter the Number 1 :");
num1=scanner.nextInt();
System.out.print("Enter the Number 2 :");
num2=scanner.nextInt();
System.out.print("Division = " + (num1/num2));
break;
default:
System.out.print("Invalid Choice...Please Try Again !!!");
}
}
}
A program that reads the user input and print the corresponding gender.
When the user inputs M or m it outputs Male
When the user inputs F or f it outputs Female
When the user inputs an invalid letter it outputs Unspecified Gender.

import java.util.Scanner;
public class Read_Gender
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String gender;

System.out.printf("Enter the Gender(M/F) : ");


gender = scanner.nextLine();

switch (gender)
{
case "M":
case "m":
System.out.println("Male");
break;
case "F":
case "f":
System.out.println("Female");
break;
default:
System.out.println("Unspecified Gender.");
break;
}
}
}

You might also like