Program to simulate a menu driven calculator
Program:
import java.util.*;
class calculator
{
public static void main(String args[])
{
int a,b,n;
Scanner sc=new Scanner(System.in);
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Modulus");
System.out.println("Enter your choice");
n=sc.nextInt();
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
switch(n)
{
case 1: System.out.println("Result = "+(a+b));
break;
case 2: System.out.println("Result = "+(a-b));
break;
case 3: System.out.println("Result = "+(a*b));
break;
case 4: System.out.println("Result = "+(a/b));
break;
case 5: System.out.println("Result = "+(a%b));
break;
default: System.out.println("Enter valid choice");
}
}
}
Output:
import java.util.*;
class calculator
{
public static void main(String args[])
{
int a,b,n;
Scanner sc=new Scanner(System.in);
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Modulus");
System.out.println("Enter your choice");
n=sc.nextInt();
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
switch(n)
{
case 1: System.out.println("Result = "+(a+b));
break;
case 2: System.out.println("Result = "+(a-b));
break;
case 3: System.out.println("Result = "+(a*b));
break;
case 4: System.out.println("Result = "+(a/b));
break;
case 5: System.out.println("Result = "+(a%b));
break;
default: System.out.println("Enter valid choice");
}
}
}
Output:
Comments
Post a Comment