Perform Arithmetic Operations in Menu Driven Program using If Block:
import java.util.Scanner; class Code { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true) { System.out.println(“1. Add”); System.out.println(“2. Subtract”); System.out.println(“3. Multiply”); System.out.println(“4. Divide”); System.out.println(“5. Quit”); System.out.print(“Enter your choice : “); int ch = sc.nextInt(); if(ch==1) { System.out.println(“Enter 2 numbers : “); int a=sc.nextInt(); int b=sc.nextInt(); int c=a+b; System.out.println(a + ” + ” + b + ” = ” + c); } else if(ch==2) { System.out.println(“Enter 2 numbers : “); int a=sc.nextInt(); int b=sc.nextInt(); int c=a-b; System.out.println(a + ” – ” + b + ” = ” + c); } else if(ch==3) { System.out.println(“Enter 2 numbers : “); int a=sc.nextInt(); int b=sc.nextInt(); int c=a*b; System.out.println(a + ” * ” + b + ” = ” + c); } else if(ch==4) { System.out.println(“Enter 2 numbers : “); int a=sc.nextInt(); int b=sc.nextInt(); int c=a/b; System.out.println(a + ” / ” + b + ” = ” + c); } else if(ch==5) { System.out.println(“End of Program”); System.exit(1); } else { System.out.println(“Invalid Choice”); } } } } |