Q2)WAP to accept two numbers and works according to the following menu(menu
driven with switch case]
1)sum
2)product
3)largest
Answers
Explanation:
largest. ......... ..........
Answer:
import java.util.*;
public class Test {
public static void main(String args[]) {
char input = '\u0000';
int a = 0, b = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Calculation | Code");
System.out.println("Sum | S");
System.out.println("Product | P");
System.out.println("largest | L");
System.out.print("Enter code: ");
input = sc.next().charAt(0);
if (input == 'S' || input == 's' || input == 'P' || input == 'p' || input == 'l' || input == 'L') {
System.out.print("Enter 1st number: ");
a = sc.nextInt();
System.out.print("Enter 2nd number: ");
b = sc.nextInt();
} else {
System.err.println("Code you entered is invalid: " + input);
System.exit(0);
}
switch (input) {
case 's':
case 'S':
System.out.println("Sum is " + (a + b));
break;
case 'p':
case 'P':
System.out.println("Difference is " + (a - b));
break;
case 'l':
case 'L':
System.out.println(Math.max(a, b) + " is greater tha " + Math.min(a, b));
}
}
}