Software & Finance





Java - Switch Case Example for Simple Arithmetic Operations





I have given here a switch case statement example in Java. Switch Case works better where are many if else conditions required. It gives better clarity and easy code maintenance.

 

The following source code will accept the user choice for addition, subtraction, multiplication and division. And then it will accept two numbers as input then uses switch case to perform the computation and print the result.

 

Source Code


import java.lang.*;

import java.util.*;

import java.io.*;

 

 

class SwitchCaseExample

{

      public static String ReadString()

      {

           try

           {

                 InputStreamReader input = new InputStreamReader(System.in);

                 BufferedReader reader = new BufferedReader(input);

                 return reader.readLine();

           }

           catch (Exception e)

           {

 

                 e.printStackTrace();

                 return "";

           }

      }

 

 

      public static int ReadInteger()

      {

           try

           {

                 InputStreamReader input = new InputStreamReader(System.in);

                 BufferedReader reader = new BufferedReader(input);

                 return Integer.parseInt(reader.readLine());

           }

           catch (Exception e)

           {

 

                 e.printStackTrace();

                 return 0;

           }

      }

     

     

 

      public static void main(String[] args)

      {

         int a, b, opcode, result;

        

         System.out.println("Program for Addition, Subtraction, Multiplication and Division\n");

        

         while (true)

         {

            System.out.print("Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: ");

            opcode = ReadInteger();

 

            if (opcode < 1 || opcode > 4)

               return;

 

            System.out.print("Enter First Number:");

            a = ReadInteger();

            System.out.print("Enter Second Number:");

            b = ReadInteger();

 

            switch (opcode)

            {

               case 1:

                  result = a + b;

                  System.out.format("%d + %d = %d\n", a, b, result);

                  break;

 

               case 2:

                  result = a - b;

                  System.out.format("%d - %d = %d\n", a, b, result);

                  break;

 

               case 3:

                  result = a * b;

                  System.out.format("%d * %d = %d\n", a, b, result);

                  break;

 

               case 4:

                  result = a / b;

                  System.out.format("%d / %d = %d\n%d %% %d = %d", a, b, result, a, b, a % b);

                  break;

               default:

                  break;

            }

         }

      }

}

 

Output