Calculator in Java

In this article we are going to create a simple calculator using java

Steps to Create Calculator in Java

  • Take input for first number.
  • Take input for second number.
  • Take operator input for performing operation.
  • Using switch() in java perform the operation according to the operator.
  • Then print the result.

Java Calculator Code


import java.util.Scanner;

public class JavaCalculator {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first no");
        int a = sc.nextInt();
        System.out.println("Enter the second no");
        int b =sc.nextInt();
        System.out.println("enter the operation(+,-,/,*,%) ");
        String c = sc.next();
        switch(c) {
            case "+":
                System.out.println(a+b);
                break;
            case "-":
                System.out.println(a-b);
                break;
            case"*":
                System.out.println(a*b);
                break;
            case"/":
                System.out.println(a/b);
                break;
            case"%":
                System.out.println(a%b);
                break;
            default:
                System.out.println("wrong symbol enter");

        }

    }

}


we created a simple calculator using switch statement in java

Similar Java Tutorials

Leave a Comment