20.Write a menu driven program to find the sum difference and product of two numbers. Perform the tasks accordingly; Enter '+' to find the sum '- to find the difference and '*' to find the product of two numbers. by using switch case
Answers
Answer:
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your choice : ");
System.out.println("1. ADDITION");
System.out.println("2. SUBTRACTION ");
System.out.println("3. MULTIPLICATION ");
int n=sc.nextInt();
System.out.println("ENTER FIRST NUMBER ");
int a=sc.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b=sc.nextInt();
double result=0;//'result' will store the result of operation
switch(n)
{
case 1:
result=a+b;
break;
case 2:
result=a-b;
break;
case 3:
result=a*b;
break;
default:
System.out.println("YOU HAVE ENTERED A WRONG CHOICE");
}
System.out.println("RESULT = "+result);
}
}
Step-by-step explanation:
Hope it helps :-)
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your choice : ");
System.out.println("1. ADDITION");
System.out.println("2. SUBTRACTION ");
System.out.println("3. MULTIPLICATION ");
int n=sc.nextInt();
System.out.println("ENTER FIRST NUMBER ");
int a=sc.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b=sc.nextInt();
double result=0;//'result' will store the result of operation
switch(n)
{
case 1:
result=a+b;
break;
case 2:
result=a-b;
break;
case 3:
result=a*b;
break;
default:
System.out.println("YOU HAVE ENTERED A WRONG CHOICE");
}
System.out.println("RESULT = "+result);
}
}
Step-by-step explanation:
Hope it helps :-)
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your choice : ");
System.out.println("1. ADDITION");
System.out.println("2. SUBTRACTION ");
System.out.println("3. MULTIPLICATION ");
int n=sc.nextInt();
System.out.println("ENTER FIRST NUMBER ");
int a=sc.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b=sc.nextInt();
double result=0;//'result' will store the result of operation
switch(n)
{
case 1:
result=a+b;
break;
case 2:
result=a-b;
break;
case 3:
result=a*b;
break;
default:
System.out.println("YOU HAVE ENTERED A WRONG CHOICE");
}
System.out.println("RESULT = "+result);
}
}
Explanation: