Java program to generate a calculator using the switch case

The following program accepts two integer variables, takes an operator regarding the operation. According to the selected operator, the program performs the respective operation and print the result.


import java.util.Scanner;
public class ab39_CalculatorUsingSwitch {
   public static void main(String args[]) {
      Scanner sc = new Scanner(;
      System.out.println("Enter value of 1st number ::");
      int a = sc.nextInt();

      System.out.println("Enter value of 2nd number ::");
      int b = sc.nextInt();

      System.out.println("Select operation");
      System.out.println("Addition-a: Subtraction-s: Multiplication-m: Division-d: ");
      char ch =;
      switch(ch) {
         case 'a' :
         System.out.println("Sum of the given two numbers: "+(a+b));
         case 's' :
         System.out.println("Difference between the two numbers: "+(a-b));
         case 'm' :
         System.out.println("Product of the two numbers: "+(a*b));
         case 'd' :
         System.out.println("Result of the division: "+(a/b));
         default :
         System.out.println("Invalid grade");


Enter value of 1st number ::
Enter value of 2nd number ::
Select operation
Addition-a: Subtraction-s: Multiplication-m: Division-d:
Sum of the given two numbers: 137

Updated on: 21-Jun-2024

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started