Coders Packet

Simple Calculator Using Java

By MASNA VISHNUDEV

In this module, we are going to make a simple calculator using Java. In this, we can perform different operations like addition, subtraction, multiplication, and division.

Implementation of calculator

In this, the program takes multiple parameters as inputs and performs the operations on them.

The various operations are :

 1.Addition

 2.Subtraction

 3.Multiplication

 4.Division

To build this program switch..case is used.

The following code gives a complete description.

import java.util.Scanner;

import static java.lang.Integer.*;

class Calculator {
    public static void main(String[] args) {

        int operator;
        Double number1, number2, result;


        Scanner input = new Scanner(System.in);


        do {


            System.out.println("\nEnter your Choice: \n1.Addition \n2.Subtraction \n3.Multiplication \n4.Division \n5.exit");
            operator = input.nextInt();
           

                System.out.println("Enter first number");
                number1 = input.nextDouble();

                System.out.println("Enter second number");
                number2 = input.nextDouble();

                switch (operator) {


                    case 1:
                        result = number1 + number2;
                        System.out.println(number1 + " + " + number2 + " = " + result);
                        break;


                    case 2:
                        result = number1 - number2;
                        System.out.println(number1 + " - " + number2 + " = " + result);
                        break;


                    case 3:
                        result = number1 * number2;
                        System.out.println(number1 + " * " + number2 + " = " + result);
                        break;


                    case 4:
                        result = number1 / number2;
                        System.out.println(number1 + " / " + number2 + " = " + result);
                        break;
                    case 5:
                        System.out.println("Execution Successful");
                        break;

                    default:
                        System.out.println("Enter a valid choice");
                        break;
                }

        }while(operator!=5);

       input.close();
    }
}

Output:

 Explanation: Here, we have used the class to take 3 inputs from the user.

  • choice - specifies the operation to be performed
  • number1/number2 - operands to perform an operation on

Here, we performed addition operation. In the same way, we can perform the remaining operations.

By using this calculator program we can perform multiple operations at once. After completion, we can exit with the exit operation.

And this is all about the simple calculator using java.

                                                                                  Thank you.

 

 

Download project

Reviews Report

Submitted by MASNA VISHNUDEV (vishnudevmasna)

Download packets of source code on Coders Packet