Write a menu driven program to make a calculator. Accept the sign as user's choice to perform the
following tasks. Also accept 2 values from user.
Option list- sum, difference, product, quotient, remainder.
Answers
- I assumed you want the answer in java programming language with respect to ICSE Board.
- Menu driven programs involve switch...case statements.
- I am using java.util.Scanner to accept the required things.
import java.util.Scanner;
public class Calculator{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in); //Scanner object is created.
System.out.println("Enter 2 numbers for calculations - ");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Enter the sign for the operation (*,+,-,/,%) :-");
char op = sc.nextLine().charAt(0); //Used to accept character.
switch(op){ //switch...case begins here
case '+':
int s=a+b;
System.out.println("The sum is -" +s);
break;
case '-':
int d=a-b;
System.out.println("The difference is -" +d);
break;
case '*':
int p=a*b;
System.out.println("The product is -" +p);
break;
case '/':
int q=a/b;
System.out.println("The quotient is -" +q);
break;
case '%':
int r=a%b;
System.out.println("The remainder is -" +r);
break;
default: //default case for error handling
System.out.println("Incorrect input.");
} //switch ends
}//main() method ends
} //class ends