In this Program we are making a basic calculator that performs addition, subtraction, multiplication and division dependent on the user input. The program takes the estimation of both the numbers (entered by user) and after that user is approached to enter the decision , dependent on the input program plays out the chose operation on the entered numbers utilizing switch case
Java Program To Implement Calculator Using Switch Case
import java.util.Scanner;
class Calculator
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b,c;
System.out.print("Enter 2 numbers : ");
a = sc.nextInt();
b = sc.nextInt();
do{
System.out.println("1:Addition\n2:Subtraction\n3:Multiplication\n4:Division\n5:Exit");
c = sc.nextInt();
switch (c) {
case 1 : System.out.println("Sum : " + (a+b)); break;
case 2 : System.out.println("Difference : " + (a-b)); break;
case 3 : System.out.println("Product : " + (a*b)); break;
case 4 : System.out.println("Quotient : " + (a/b)); break;
}
}while(c!=5);
}
}
Output:
Enter 2 numbers : 9 3
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Division
5 : Exit
1
Sum : 12
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Division
5 : Exit
4
Quotient : 3
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Division
5 : Exit
5
tags : java , java programs , java tutorialspoint , tutorialspoint , c , programs of c , c++ , programs of c++ , java code , switch , codecamp
Java Program To Implement Calculator Using Switch Case
import java.util.Scanner;
class Calculator
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b,c;
System.out.print("Enter 2 numbers : ");
a = sc.nextInt();
b = sc.nextInt();
do{
System.out.println("1:Addition\n2:Subtraction\n3:Multiplication\n4:Division\n5:Exit");
c = sc.nextInt();
switch (c) {
case 1 : System.out.println("Sum : " + (a+b)); break;
case 2 : System.out.println("Difference : " + (a-b)); break;
case 3 : System.out.println("Product : " + (a*b)); break;
case 4 : System.out.println("Quotient : " + (a/b)); break;
}
}while(c!=5);
}
}
Output:
Enter 2 numbers : 9 3
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Division
5 : Exit
1
Sum : 12
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Division
5 : Exit
4
Quotient : 3
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Division
5 : Exit
5
tags : java , java programs , java tutorialspoint , tutorialspoint , c , programs of c , c++ , programs of c++ , java code , switch , codecamp