32+ Java Calculator Examples

This post contains a total of 32+ Hand-Picked Java Calculator Examples. All the calculators are made purely using Java Programming language.

You can use the source code of these Java calculators for educational use with credits to the owner, if you need it for some other purpose then you may contact the original owner for permission.

Related Posts

Click a Code to Copy it, and Hover over a video to play it.

1. By Rapt[#Demure]

Java calculator by Rapt[#Demure]. The program first asks you for the first and the second number, after that it asks you for the operator you want to use, the result is displayed after that. ( Source )

import java.util.Scanner;
 public class JavaExample
  { 
  public static void main(String[] args)
   { 
   	double num1, num2; 
   Scanner scanner = new Scanner(System.in); System.out.print("Enter first number:");
    /* We are using data type double so that user * can enter integer as well as floating point * value */ 
    num1 = scanner.nextDouble(); System.out.print("Enter second number:"); 
    num2 = scanner.nextDouble(); System.out.print("Enter an operator (+, -, *, /): "); 
    char operator = scanner.next().charAt(0); scanner.close(); 
    double output; 
    switch(operator) 
    { 
    case '+': 
    	output = num1 + num2;
     break; 
     case '-': 
     output = num1 - num2;
      break; 
      case '*': 	
      output = num1 * num2; 
      break; 
      case '/': 	
      output = num1 / num2; 
      break; 
      /* If user enters any other operator or char apart from * +, -, * and /, then display an error message to user * */ 
      default: 
      System.out.printf("You have entered wrong operator");
       return; 
      } 
      System.out.println(num1+" "+operator+" "+num2+": "+output); 
      } 
      }

2. By BroFar

Made by BroFar. Enter the calculation in different lines, first number, operator then the second number. ( Source )

import java.util.Scanner; 
import java.lang.Math;

public class Program
{   
/* Enter number 
   operator
   second number submit */
   
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in); 
    float b = scanner.nextFloat(); 
    char operator = scanner.next().charAt(0);
    float c = scanner.nextFloat();
    
    scanner.close(); 
    
    switch (operator) { 
    case '+': /* addition */
    System.out.println( b + "+" + c + "= " + (b+c)); 
    break; 
    case '-': /* subtract */
    System.out.println( b + "-" + c + "= " + (b-c)); 
    break; 
    case '*': /* multiplication */
    System.out.println( b + "*" + c + "= " + (b*c)); 
    break; 
    case '/': /* division */
    int a = Math.round(b/c);
    System.out.println( b + "/" + c + "= " + a + "r" + (b%c)); 
    break; 
    default:
    System.out.println("Operation or input is not correction...\n                Please try again");
    break;
    }
  }
}

3. By NiKiπŸŒΈπŸ’•

Basic java calculator made by NiKiπŸŒΈπŸ’•. The input needs to be in the format of 1st number in first line, then second number in second line, after that the operator in the last line. ( Source )

import java.util.Scanner;

public class Calculator{
	public static void main(String[] args) {
	    System.out.println("~~SIMPLE CALCULATOR~~");
	    System.out.println("---------------------\n");

	    
		Scanner input = new Scanner(System.in);
		if(!input.hasNextDouble()){
		    System.out.println("Please give 2 numbers and 1 operator(+,-,*,/) \nin the next format:\n5\n2\n+");
		    return;
		}
		
		double num1 = input.nextDouble();  //integer, float
		//char operator = input.next().charAt(0);  //πŸ‘‰Activate this line for 5 + 2 input format
		double num2 = input.nextDouble();
		double result;

	   char operator = input.next().charAt(0);  //πŸ‘‰Disactivate this line for 5 + 2 input format charAt(0) returns the first character
        switch(operator)
        {
            case '+':
            	result = num1 + num2;
                break;

            case '-':
            	result = num1 - num2;
                break;

            case '*':
            	result = num1 * num2;
                break;

            case '/':
            	result = num1 / num2;
                break;

            default:
                System.out.println("Please give 2 numbers and 1 operator(+,-,*,/) \nin the next format:\n5\n2\n+");
                return;
        }

        System.out.println(num1+" "+operator+" "+num2+" = "+result);
	}
}

4. By Eliya Ben Baruch

Made by Eliya Ben Baruch. The calculation needs to be entered in one single line with and without the use of spaces in between. Like for example – 1+1 or 2 + 2. ( Source )

import java.util.*;
public class Calculator {
public static Scanner reader = new Scanner(System.in);
public static void main(String[] args) {
    System.out.println("Enter your exercise:");
    String exercise = reader.nextLine().replace(" ", ""), recExercise = exercise, mathSymbols = "+-*/().^", numbers = "0123456789";
    int countSteps = 1;
    if (!regularExpression(exercise, numbers, mathSymbols)) { 
        System.out.println("The expression is wrong, please enter again:");
        main(new String[]{});
    }
    else {
        while (!noSymbols(exercise, mathSymbols)) {
            exercise = choosePart(exercise, mathSymbols);
            System.out.println(countSteps++ + ") " + exercise);
        }
        System.out.println("Answer is: " + recExercise + " = " + exercise);
    }
}
public static boolean regularExpression(String exercise, String numbers, String mathSymbols) {
    Stack<String> regular = new Stack<String>();
    try {
        for (int i = 0; i < exercise.length(); i++) { // Checking if the expression is regular
            if (exercise.charAt(i) == '(')
                regular.push("(");
            else if (exercise.charAt(i) == ')')
                regular.pop();
        }
    }
    catch(Exception EmptyStackException) {
        return false;
    }
    if (regular.size() > 0 || "*/)^".indexOf(exercise.charAt(0)) != -1 || (numbers.indexOf(exercise.charAt(0)) == -1 && mathSymbols.indexOf(exercise.charAt(0)) == -1) || (numbers.indexOf(exercise.charAt(exercise.length() - 1)) == -1 && exercise.charAt(exercise.length() - 1) != ')')) {
        return false;
    }
    for (int i = 0; i < exercise.length() - 1; i++) {
        if ((mathSymbols.indexOf(exercise.charAt(i)) != -1 && mathSymbols.indexOf(exercise.charAt(i + 1)) != -1 && exercise.charAt(i + 1) != '(' && exercise.charAt(i) != ')') || (numbers.indexOf(exercise.charAt(i)) == -1 && mathSymbols.indexOf(exercise.charAt(i)) == -1))
            return false;
    }
    return true;
}
public static String choosePart(String exercise, String mathSymbols) {
    int start = 0, end = exercise.length(), count = 1;
    if (exercise.indexOf('(') != -1) {
        start = exercise.indexOf('(');
        for (int i = start + 1; i < exercise.length() && count > 0; i++) {
            if (exercise.charAt(i) == '(')
                count++;
            if (exercise.charAt(i) == ')')
                count--;
            end = count == 0 ? i : end;
        }
        return !noSymbols(exercise.substring(start + 1, end), mathSymbols) ? exercise.substring(0, start + 1) + choosePart(exercise.substring(start + 1, end), mathSymbols) + ")" : exercise.replace(exercise.substring(start, end + 1), exercise.substring(start + 1, end)) + exercise.substring(end + 1, exercise.length());
    }
    else if (exercise.indexOf('^') != -1) {
        for (int i = exercise.indexOf('^') - 1; i > 0 && start == 0; i--) {
            start = mathSymbols.indexOf(exercise.charAt(i - 1)) != -1 && exercise.charAt(i - 1) != '.' ? i : start;
            }
        for (int i = exercise.indexOf('^') + 1; i < exercise.length() && end == exercise.length(); i++)
            end = mathSymbols.indexOf(exercise.charAt(i)) != -1 && exercise.charAt(i) != '.' ? i : end;
        return exercise.replace(exercise.substring(start, end), calculate(exercise.substring(start, end), exercise.substring(start, end).indexOf('^')));
    }
    else if (exercise.indexOf('/') != -1) {
        for (int i = exercise.indexOf('/') - 1; i > 0 && start == 0; i--) {
            start = mathSymbols.indexOf(exercise.charAt(i - 1)) != -1 && exercise.charAt(i - 1) != '.' ? i : start;
            }
        for (int i = exercise.indexOf('/') + 1; i < exercise.length() && end == exercise.length(); i++)
            end = mathSymbols.indexOf(exercise.charAt(i)) != -1 && exercise.charAt(i) != '.' ? i : end;
        return exercise.replace(exercise.substring(start, end), calculate(exercise.substring(start, end), exercise.substring(start, end).indexOf('/')));
    }
    else if (exercise.indexOf('*') != -1) {
        for (int i = exercise.indexOf('*') - 1; i > 0 && start == 0; i--) {
            start = mathSymbols.indexOf(exercise.charAt(i - 1)) != -1 && exercise.charAt(i - 1) != '.' ? i : start;
            }
        for (int i = exercise.indexOf('*') + 1; i < exercise.length() && end == exercise.length(); i++)
            end = mathSymbols.indexOf(exercise.charAt(i)) != -1 && exercise.charAt(i) != '.' ? i : end;
        return exercise.replace(exercise.substring(start, end), calculate(exercise.substring(start, end), exercise.substring(start, end).indexOf('*')));
    }
    else if (exercise.indexOf('+') != -1) {
        for (int i = exercise.indexOf('+') - 1; i > 0 && start == 0; i--) {
            start = mathSymbols.indexOf(exercise.charAt(i - 1)) != -1 && exercise.charAt(i - 1) != '.' ? i : start;
            }
        for (int i = exercise.indexOf('+') + 1; i < exercise.length() && end == exercise.length(); i++)
            end = mathSymbols.indexOf(exercise.charAt(i)) != -1 && exercise.charAt(i) != '.' ? i : end;
        return exercise.replace(exercise.substring(start, end), calculate(exercise.substring(start, end), exercise.substring(start, end).indexOf('+')));
    }
    else if (exercise.indexOf('-') != -1) {
        for (int i = exercise.indexOf('-') - 1; i > 0 && start == 0; i--) {
            start = mathSymbols.indexOf(exercise.charAt(i - 1)) != -1 && exercise.charAt(i - 1) != '.' ? i : start;
            }
        for (int i = exercise.indexOf('-') + 1; i < exercise.length() && end == exercise.length(); i++)
            end = mathSymbols.indexOf(exercise.charAt(i)) != -1 && exercise.charAt(i) != '.' ? i : end;
        return exercise.replace(exercise.substring(start, end), calculate(exercise.substring(start, end), exercise.substring(start, end).indexOf('-')));
    }
    return "";
}
public static String calculate(String exercise, int symbolIndex) {
    Double operand1 = Double.parseDouble(exercise.substring(0, symbolIndex)), operand2 = Double.parseDouble(exercise.substring(symbolIndex + 1, exercise.length()));
    if (exercise.indexOf('^') != -1) {
        return "" + Math.pow(operand1, operand2);
    }
    else if (exercise.indexOf('/') != -1) {
        return "" + (operand1 / operand2);
    }
    else if (exercise.indexOf('*') != -1) {
        return "" + (operand1 * operand2);
    }
    else if (exercise.indexOf('+') != -1) {
        return "" + (operand1 + operand2);
    }
    else {
        return "" + (operand1 - operand2);
    }
}
public static boolean noSymbols(String exercise, String mathSymbols) {
    for (int i = 1; i < exercise.length(); i++)
        if (mathSymbols.indexOf(exercise.charAt(i)) != -1 && exercise.charAt(i) != '.')
            return false;
    return true;
}
}

5. By Carlos

Made by Carlos. Enter first number in first line then enter the second number in second line, after that enter what you want to do, ( sum, subtract, divide, multiply, power, etc. ). ( Source )

import java.util.Scanner;

public class Calculator{
    public static void main(String[] args){
        double a;
        double b;
        String c; 
        Scanner sc = new Scanner(System.in);
          a= sc.nextDouble();
          b= sc.nextDouble();
            sc.nextLine();
          c= sc.nextLine();
          c=c.toLowerCase();
        System.out.print(" ans=");
        if(c.equals("sum")){
            System.out.print(a+b);
        }
        else if(c.equals("substract")){
            System.out.print(a-b);
        }
        else if(c.equals("multiply")){
            System.out.print(a*b);
        }
        else if(c.equals("divide")){
            System.out.print(a/b);
        }
        else if(c.equals("module")){
            System.out.print(a%b);
        }
        else if(c.equals("square root of a")){
            System.out.print(Math.sqrt(a));
        }
        else if(c.equals("power")){
            System.out.print(Math.pow(a,b));
        }
        else if(c.equals("logarithm of a")){
            System.out.print(Math.log(a));
        }
    }
}

6. By MIACniK

Made by MIACnik. The calculation have to be entered in one single line with spaces, like 2 + 2. Without spaces in between the operator and number wont work. ( Source )

import java.util.Scanner;
class cal {
    static void foo(String name){
        System.out.println("         " + name);
    }
    double firstNumber,secondNumber,result;
    String sign;
    cal(){
        Scanner ja = new Scanner(System.in);
        firstNumber = ja.nextDouble(); sign = ja.next(); secondNumber = ja.nextDouble();
    }
}
class Sd extends cal {
  void calculate(){
        switch(sign){
        
        case "+":
            result = firstNumber + secondNumber;
            System.out.println(firstNumber + sign + secondNumber + "=" + result);
            break;
            
             case "-":
            result = firstNumber - secondNumber;
            System.out.println(firstNumber + sign + secondNumber + "=" + result);
            break;
            
             case "*":
            result = firstNumber * secondNumber;
            System.out.println(firstNumber + sign + secondNumber + "=" + result);
            break;
            
             case ":":
                 if(secondNumber == 0){
                     System.out.println("ERROR 305");
                 }else{
            result = firstNumber / secondNumber;
            System.out.println(firstNumber + sign + secondNumber + "=" + result);
                 }
            break;
        };
    }   
}
public class Program
{
	public static void main(String[] args) {
cal.foo("calculator");
	Sd s = new Sd();
	s.calculate();
	}
}

7. By Ali Han Demir

Made by Ali Han Demir. First enter the first number after that enter the second number, after that enter what operator you want to use. ( Source )

import java.util.Scanner;

public class Program
{
//input will be like 18 24 Multiply

    public static void main(String[] args) {
        
     Scanner sc = new Scanner(System.in);  
     System.out.println("Enter first number "); 
     int number1 = sc.nextInt();
     System.out.println("Enter second number  ");
     int number2 = sc.nextInt();
     
     System.out.println("Please choose what you want to calculate:Plus ,Minus, Multiply,  Division");
      
      String process = sc.next();
      
      double result =0 ;
      
      switch(process)
      {
          case "Plus":
          result = sum(number1,number2);
          break;
        
          case "Minus":
          result = minus(number1,number2);
          break;
          
          case "Multiply":
          result = multiply(number1,number2);
          break;
          
          case "Division":
          result = division(number1,number2);
          break;
      }
      
      System.out.println("Result of two number is : "+ result);
        
    }
    
    public static int sum(int a,int b){
        return a+b;
    }
    
    public static int minus(int a,int b){
        return a-b;
    }
    
    public static int multiply(int a,int b){
        return a*b;
    }
    
    public static double division(double a,double b){
        return a/b;
    }
}

8. By Safura Patel

Made by Safura Patel. You have to enter the two numbers of the calculation with spaces in between and the program will print out all the results using all the operators. ( Source )

/* 
 ENTER THE TWO VALUES
      Ex:- 5  2
*/

/* 
  NOTE :This program only deals with the Integer data type. 
*/

import java.util.Scanner;
public class Program
{
    public static void main(String[] args)
    {
    
 Scanner value=new Scanner(System.in);
 int n1= value.nextInt();
 int n2= value.nextInt();
 int a = n1+n2;
 int b = n1-n2;
 int c = n1*n2;
 int d = n1/n2;
 int e = n1%n2;
 
  System.out.println("\n"); 
  System.out.println("\t\t\t" +n1+ " + " +n2+ " = " +a);
  System.out.println("\t\t\t" +n1+ " - " +n2+ " = " +b);
  System.out.println("\t\t\t" +n1+ " * " +n2+ " = " +c);
  System.out.println("\t\t\t" +n1+ " / " +n2+ " = " +d);
 System.out.println("\t\t\t" +n1+ " % " +n2+ " = " +e);   
   
   
System.out.println("\n\n\t Thank you for visiting πŸ«πŸ€— \n\n\t\t Β© Safura Patel πŸ’«");   
   
    }
}

9. By Naimish Shah

Made by Naimish Shah. You have to first select what operator you want to use, after that you have to enter the two numbers in the same line with space in between. ( Source )

//Enter your choice (A,S,M,D,R,E)
//(A)Addition
//(S)SUBTRACTION
//(M)Multiplication
//(D)Division
//(R)Remainder
//(E)Exit
//Enter two numbers in diffrent line
import java.util.*;
public class Calculator
{
    public static void main(String args [])
    {
        Scanner s=new Scanner(System.in);
        double num1,num2,result=0;
        System.out.println("Calculator menue");
        System.out.println("=================");
        System.out.println("(A)Addition");
        System.out.println("(S)SUBTRACTION");
        System.out.println("(M)Multiplication");
        System.out.println("(D)Division");
        System.out.println("(R)Remainder");
        System.out.println("(E)Exit");
        System.out.println();
        
        System.out.println("Enter your choice (A,S,M,D,R,E)");
        char choice=s.next().charAt(0);
        System.out.println("Enter the two numbers");
        switch(choice)
        {
            case 'A':
            num1=s.nextInt();
            num2=s.nextInt();
            result=num1+num2;
            break;
            
            case 'S':
            num1=s.nextInt();
            num2=s.nextInt();
            result=num1-num2;
            break;
            
            case 'M':
            num1=s.nextInt();
            num2=s.nextInt();
            result=num1*num2;
            break;
           
            case 'D':
            num1=s.nextInt();
            num2=s.nextInt();
            result=num1/num2;
            break;
            
            case 'R':
            num1=s.nextInt();
            num2=s.nextInt();
            result=num1%num2;
            break;
            
            case 'E':
            System.out.println("Thanks for using calculator");
            System.exit(0);
            
            default:
            System.out.println("****Invalid operation****");
            System.exit(0);
        }
        System.out.println("Result : "+result);
        System.out.println("Program made by NAIMISH SHAH");
    }
}

10. By Tushit Maheshwari

Made by Tushit Maheshwari. The input needs to be in the same line, enter first number then the operator after that the second number, all without spaces in between. ( Source )

//Made by Tushit Maheshwari...
//The instruction are type a Mathematical expression containing 2 numbers.
//example:
//23214/23 or 45+6543 or 43-21 or 34 X 4 or 89*32  or 34x5 etc. etc.
//Note:My program only deals with the Integer values.
// Answers will be of Integer type.
import java.util.Scanner;
class calculator
{
    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        System.out.println("Feed the expression in:");
        String s=in.nextLine().trim();
        for(char f='A';f<='Z';f++)
        {
            if(f=='X')
            {
                continue;
            }
            if(s.contains(String.valueOf(f)))
            {
                System.out.println("Sorry I can not solve an algebric expression.");
                System.exit(0);
            }
            }
                for(char f='a';f<='z';f++)
        {
            if(f=='x')
            {
                continue;
            }
            if(s.contains(String.valueOf(f)))
            {
                System.out.println("Sorry I can not solve an algebric equation.");
                System.exit(0);
            }
            }
        int count=0;
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)=='+'||s.charAt(i)=='-'||s.charAt(i)=='/'||s.charAt(i)=='X'||s.charAt(i)=='*'||s.charAt(i)=='x')
            {
                count=i;
                break;
            }
        }
        if(count==0)
        {
            System.out.println("Sorry but what to solve?\n Read the instructions properly");
            System.exit(0);
        }
        for(int i=count+1;i<s.length();i++)
        {
            if(Character.isDigit(s.charAt(i))==true||Character.isLetter(s.charAt(i))==true)
            {
                continue;
            }
            else{
                System.out.println("Sorry wrong symbols used \nPlease read the instruction.");
                System.exit(0);
            }
        }

        int a1=Integer.valueOf(s.substring(0,count));
        int b=Integer.valueOf(s.substring((count+1),s.length()));
        char ch='?';
        {

        }
        if(s.contains("+"))
        {
            ch='A';
        }
        else if(s.contains("-"))
            {
                ch='S';
            }
            else if(s.contains("*")||s.contains("X")||s.contains("x"))
            {
                ch='M';
            }
            else if(s.contains("/"))
            {
                ch='D';
            }
        System.out.println("Input: "+s);        
        switch(ch)
        {
            case 'A':
                System.out.println("The answer is: "+(a1+b));
                break;
            case 'S':
                System.out.println("The answer is: "+(a1-b));
                break;
            case 'M':
                System.out.println("The answer is: "+(a1*b));
                break;
            case 'D':
                if(b==0)
                {
                    System.out.println("Try Again");
                    break;
                }
                else
                {
                System.out.println("The answer is: "+(a1/b));
                break;
                }
        }
    }
}

11. By Fau_zi

Basic Java Calculator made by Fau_zi. You have to first enter the first number, after that the second number and at last the operator that you want to use. ( Source )

import java.util.Scanner;
public class Calculator
{
    public static void main(String[] args) {
        Scanner n = new Scanner(System.in);
        System.out.println("Enter the first number");
        int num1 = n.nextInt();
        System.out.println("Enter the second number");
        int num2 = n.nextInt();
        System.out.println("Select a symbol (+,-,*,/)");
        String sum = n.next();
        int result;
        switch(sum){
            case"+":result=num1+num2;
            System.out.printf("Addition of %d and %d is %d",num1,num2,result);
            break;
            
            case"-":result=num1-num2;
            System.out.printf("Subtraction of %d and %d is %d",num1,num2,result);
            break;
            
            case"*":result=num1*num2;
            System.out.printf("Multiplication of %d and %d is %d",num1,num2,result);
            break;
            
            case"/":result=num1/num2;
            System.out.printf("Division of %d and %d is %d",num1,num2,result);
            break;
            
            default:
            System.out.println("No pain no gain ☺️");
        }
    }
}

12. By MiFiX

Made by MiFiX. Change int a and b for regular calculation that will give you the output using all the basic operators. ( Source )

public class Program
{
    public static void main(String[] args) {
    //like please :)
        int a =100 ;
        int b =50  ;
        long s = a + b;
        long p = a * b;
        int c = a / b;
        int r = a - b;
        int c1 = b / a;
        int r1 = b - a;
System.out.println("a+b="+s+" a*b="+p+" a/b="+c+" a-b="+r+" b/a="+c1+" b-a="+r1);
        int aa =100     ;
        int ab =100     ;
        int i = 5     ;
        int z = 5     ;
        int q = 5     ;
        int l = 5     ;
        int x = 5     ;
        int w = 5     ;
        int d = 5     ;
        int v = 5     ;
        long as =aa +  i  ;
        long bs =ab +  z  ;
        long ap =aa *  q  ;
        long bp =ab *  l  ;
        int ac =aa /   x  ;
        int bc =ab /   w  ;
        int ar =aa -   d  ;
        int br =ab -   v  ;
        int ac1 =x /   aa ;
        int bc1 =w /   ab ;
        int ar1 =d -   aa ;
        int br1 =v -   ab ; System.out.println(aa+"+"+i+"="+as+" "+ab+"+"+z+"="+bs+" "+aa+"*"+q+"="+ap+" "+ab+"*"+l+"="+bp);
System.out.println(aa+"/"+x+"="+ac+" "+ab+"/"+w+"="+bc+" "+aa+"-"+d+"="+ar+" "+ab+"-"+v+"="+br);
System.out.println( x+"/"+aa+"="+ac1+" "+w+"/"+ab+"="+bc1+" "+d+"-"+aa+"="+ar1+" "+v+"-"+ab+"="+br1);
    } 
}
//twelve times better than usual
//Hi!!

13. By Miljan Stojanovic

Made by Miljan Stojanovic. You have to enter the calculation in single line with spaces between the first and second number and the operator. ( Source )

import java.io.IOException;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
/*
    Enter first number, operations (+,-,*,/,% or ^) and second number with a space between them.
*/
public class Calculator {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        try {
            double x = input.nextDouble();

            String operation = input.next();
            if (!operation.equals("+") && !operation.equals("-") && !operation.equals("*") && !operation.equals("/")
                    && !operation.equals("%") && !operation.equals("^"))
                throw new IOException("\nYou entered wrong operation, please try again.");

            double y = input.nextDouble();
            if (operation.equals("/") && y==0)
                throw new ArithmeticException("\nCannot divide by zero.");

            double result = 0;

            switch (operation) {
                case "+":
                    result = x + y;
                    break;
                case "-":
                    result = x - y;
                    break;
                case "*":
                    result = x * y;
                    break;
                case "/":
                    result = x / y;
                    break;
                case "%":
                    result = x % y;
                    break;
                case "^":
                    result = Math.pow(x,y);
                    break;
            }

            System.out.printf("\n%.2f %s %.2f = %.2f", x, operation, y, result);
        }catch(IOException e){
            System.out.println(e.getMessage());
        }catch(InputMismatchException e){
            System.out.println("Error!!!\nPlease enter a number.\nTry again.");
        }catch(ArithmeticException e){
            System.out.println (e.getMessage());
        }catch(NoSuchElementException e){
            System.out.println("Please try again.\nEnter first number, operations (+,-,*,/,% or ^) and second number with a space between them.");
        }catch(Exception e){
            System.out.println(e);
        }

    }

}

14. By Ankit Das

Made by Ankit Das. First enter the 1st number, after that the 2nd number and at last enter the operator you want to use. ( Source )

import java.io.*; 
public class ICSE 
{
 public static void main(String args[])throws IOException 
 { 
 InputStreamReader isr=new InputStreamReader(System.in); 
 BufferedReader br=new BufferedReader(isr); 
 System.out.println("Enter a number "); 
 int n1=Integer.parseInt(br.readLine()); 
 System.out.println("Enter another number "); 
 int n2=Integer.parseInt(br.readLine()); 
 System.out.println("Enter an operator ");
char op=(char)br.read(); 
 int ans; 
 switch(op) 
 { 
 case '+': ans=n1+n2; 
 System.out.println("Sum = "+ans); 
 break; 
 case '-': ans=Math.abs(n1-n2); 
 System.out.println("Absolute difference = "+ans); 
 break; 
 case '*': ans=n1*n2; 
 System.out.println("Product = "+ans); 
 break; 
 case '/': int g=Math.max(n1,n2); 
 int s=Math.min(n1,n2); 
 if(s!=0) 
 { 
 double q=(double)g/s; 
 System.out.println("Dividing greater by the smaller"); 
 System.out.println("Quotient = "+q); 
 } 
 else 
 { System.out.println("Division by 0 not allowed"); 
 } 
 break; 
 default: System.out.println("Wrong operator"); 
 break; 
 }//switch 
 }//main 
}//class

15. By ADVAIT CHAVAN

Made by ADVAIT CHAVAN. First enter two integer numbers with space between them, after that select the operator you want to use using numbers and not names. ( Source )

import java.util.Scanner;
public class Program
{
	public static void main(String[] args) {
	    Scanner input = new Scanner(System.in);
	    System.out.println("Enter two integer numbers:-");
	    float x = input.nextFloat();
	    float y = input.nextFloat();
	    System.out.println("You have entered " + x +" and "+ y +" as two integers numbers.");
	    System.out.println("1-ADDITION");
	    System.out.println("2-SUBTRACTION");
	    System.out.println("3-MULTIPLICATION");
	    System.out.println("4-DIVISION");
	    System.out.println("5-MODULUS");
	    Scanner choice = new Scanner(System.in);
	    System.out.println("Enter any one option from above:-");
	    int ch = input.nextInt();
	    System.out.println("You have entered  option " + ch + " as your choice of operation");
	    switch(ch){
	        case 1:
	            System.out.println("Addition of the two numbers entered is " + x+y);
	            break;
	       case 2:
	           System.out.println("Subtraction of the two numbers entered is " + (x-y));
	           break;
	       case 3:
	           System.out.println("Multiplication of the two numbers entered is " + x*y);
	           break;
	       case 4:
	           System.out.println("Division of the two numbers entered is " + x/y);
	           break;
	       case 5:
	           System.out.println("Modulus of the two numbers entered is " + x%y);
	           break;
	       default :
	           System.out.println("You have eneterd an invalid option.Please select a proper option and try again!");
	       
	    }
	    
		
	}
}

16. By Michele Zanetti

A simple calculator by Michele Zanetti. You have to enter the calculation in separate lines, first enter first number in first line, then enter operator you want to use in second line and after that the second number in the third line. ( Source )

import java.util.Scanner;

public class Program
{
    public static void main(String[] args) {
    
      double a;
      double b;
      char o;
      double result;
      
      Scanner scanner = new Scanner(System.in);
      
      a = scanner.nextDouble();
      o = scanner.next().charAt(0);
      b = scanner.nextDouble();

      scanner.close();
      
          if (o=='a') {
          
          result = a + b;
          System.out.println(""+a+" + "+b+" = "+result+"");
          result = a - b;
          System.out.println(""+a+" - "+b+" = "+result+"");
          result = a * b;
          System.out.println(""+a+" * "+b+" = "+result+"");
          result = a / b;
          System.out.println(""+a+" / "+b+" = "+result+"");
          result = a % b;
          System.out.println(""+a+" % "+b+" = "+result+"");
          result = Math.pow(a, b);
          System.out.println(""+a+" ^ "+b+" = "+result+"");
          
      } else if (o=='+') {
      
          result = a + b;
          System.out.println(result);
          
      } else if (o=='-') {
      
          result = a - b;
          System.out.println(result);
          
      } else if (o=='*') {
      
          result = a * b;
          System.out.println(result);
          
      } else if (o=='/') {
      
          result = a / b;
          System.out.println(result);
          
      } else if (o=='%') {
      
          result = a % b;
          System.out.println(result);
            
      } else if (o=='^') {
          
          result = Math.pow(a, b);
          System.out.println(result);
          
      } else {
          
          System.out.println("❌ Error!");
          
      } 
    }
}

17. By Abdul LeeπŸŽŒπŸ‡¨πŸ‡³πŸ‡³πŸ‡¬

Java calculator by Abdul LeeπŸŽŒπŸ‡¨πŸ‡³πŸ‡³πŸ‡¬. First you have to choose what operator you want to use using 1, 2, 3, 4, 5, after that you have to enter two numbers that you want to calculate, the numbers must have spaces in between them. The program restarts after giving output. ( Source )

import java.util.Scanner;
import java.io.*;
 
public class Calculator {
 
  public static void main(String[] args) {
 
    int choice;
    int x = 0;
    int y = 0;
    int sum;
     
    PrintStream out;
     
    Scanner input ;
 
    Calculator calc = new Calculator();
 
    try {
      out = new PrintStream("calclog.txt");
 
      do {
        System.out.println("Calculator Program");
        System.out.println("--------------------\n");
        System.out.println("1. Add");
        System.out.println("2. Subtract");
        System.out.println("3. Multiply");
        System.out.println("4. Divide");
        System.out.println("5. Mod");
        System.out.println("6. Power");
        System.out.println("99. End Program\n");
        System.out.println("Enter Choice: ");
 
          input = new Scanner(System.in);
        choice = input.nextInt();
 
        while ((choice < 1 || choice > 6) && choice != 99) {
 
          System.out.println("Please enter 1, 2, 3, 4, 5, or 6: ");
          choice = input.nextInt();
 
        }
 
        if (choice != 99) {
 
          System.out.println("Please enter 2 numbers only: ");
          x = input.nextInt();
          y = input.nextInt();
 
        }
 
        switch (choice) {
         
        case 1:
           
          sum = calc.add(x, y);
          System.out.printf("The sum is %d\n\n", sum);
          out.println(x + "+" + y + "=" + sum);
           
          break;
 
        case 2:
           
          sum = calc.sub(x, y);
          System.out.printf("The answer is %d\n\n", sum);
          out.println(x + "-" + y + "=" + sum);
           
          break;
 
        case 3:
           
          sum = calc.multi(x, y);
          System.out.printf("The answer is %d\n\n", sum);
          out.println(x + "*" + y + "=" + sum);
           
          break;
 
        case 4:
           
          try {
 
            sum = calc.div(x, y);
            System.out.printf("The answer is %d\n\n", sum);
            out.println(x + "/" + y + "=" + sum);
 
          } catch (Exception e) {
 
            System.out
                .println("\nError: Cannot Divide by zero\n\n");
 
          }
           
          break;
 
        case 5:
           
          sum = calc.mod(x, y);
          System.out.printf("The mod is %d\n\n", sum);
          out.println(x + "%" + y + "=" + sum);
           
          break;
 
        case 6:
           
          sum = calc.pow(x, y);
          System.out.printf("The answer is %d\n\n", sum);
          out.println(x + "^" + y + "=" + sum);
           
          break;
        }
 
 
      }
 
      while (choice != 99);
      input.close();
      System.out.println("Ending program...");
 
    }
 
    catch (Exception e) {
      System.out.println("ERROR: Some error occured");
      e.printStackTrace();
    }
 
  }
 
  public int add(int num1, int num2) {
 
    int sum;
    sum = num1 + num2;
 
    return sum;
  }
 
  public int sub(int num1, int num2) {
 
    int sum;
    sum = num1 - num2;
 
    return sum;
  }
 
  public int multi(int num1, int num2) {
 
    int sum;
    sum = num1 * num2;
 
    return sum;
  }
 
  public int div(int num1, int num2) {
 
    int sum;
    sum = num1 / num2;
 
    return sum;
  }
 
  public int mod(int num1, int num2) {
 
    int sum;
    sum = num1 % num2;
 
    return sum;
  }
 
  public int pow(int base, int exp) {
 
    int sum = 1;
 
    if (exp == 0) {
      sum = 1;
    }
 
    while (exp > 0) {
      sum = sum * base;
      exp--;
 
    }
 
    return sum;
 
  }
 
}

18. By Yogeshwaran

Made by Yogeshwaran. You have to enter the calculation in one single line, with spaces between the numbers and the operator like – 3 + 3. ( Source )

import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
       String d = user();
       System.out.println(d);
    }
    public static String user(){
       Scanner myvar = new Scanner(System.in);
    float inp1 = myvar.nextFloat();
    String c = myvar.next();
    float inp2 = myvar.nextFloat();
    switch(c){
            case "+":
            float A = inp1 + inp2;
            return "*First Number:" +" " + inp1 + "\n" + "*Second Number:" + " " + inp2 + "\n" + "*Arithmetic Operator is Addition and Result is:" + " " + A;
            case "-":
            float B = inp1 - inp2 ;
            return "*First Number:" +" " + inp1 + "\n" + "*Second Number:" + " " + inp2 + "\n" + "*Arithmetic Operator is Subtraction and Result is:" + " " + B;
            case "*":
            float C = inp1 * inp2;
            return "*First Number:" +" " + inp1 + "\n" + "*Second Number:" + " " + inp2 + "\n" + "*Arithmetic Operator is Multiplication and Result is:" + " " + C;
            case "/":
            float D = inp1 / inp2;
            return "*First Number:" +" " + inp1 + "\n" + "*Second Number:" + " " + inp2 + "\n" + "*Arithmetic Operator is Division and Result is:" + " " + D;
            case "%":
            float E = inp1 % inp2;
            return "*First Number:" +" " + inp1 + "\n" + "*Second Number:" + " " + inp2 + "\n" + "*Arithmetic Operator is Modulus and Result is:" + " " + E;
            default:
            return "Error Occured!";
            
        }

           
       }
   }

19. By Saksham

Made by Saksham. Enter the calculation in different lines, first enter first number then in second line enter the second number and at third line enter the operator. ( Source )

import java.util.Scanner;


public class MyClass{
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
       float a = sc. nextFloat();
    
     float b = sc. nextFloat();
    sc.nextLine();
    
    char operation=sc.nextLine().charAt(0);
    float result=0;
    switch(operation){
    
        case '+':
        result =a+b;
        break ;
        case '-':
        result =a-b;
        break ;
        case '*':
        result = a*b;
       break;
        case '/':
        result =a/b; 
        break; 
        default:
        System.out.println("Invalid operation");   
    }
    System .out.println ("The Result is"+" "+result);
    System.out.println ("If you liked it give it a like");    
    }
}

20. By David Akhihiero

GUI Java calculator by David Akhihiero, run the code in eclipse. ( Source )

//RUN THIS ON ECLIPSE
import javax.swing.*;
 import java.awt.*;
import java.awt.event.*;
class Calculator {
    JTextField field;
    double op;
    double op2;
    int cure;
    String x;
     String prev="null";
public void run(){
    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,500);
    frame.setTitle("Calculator by David");
    frame.setVisible(true);
    JPanel panel = new JPanel();
    frame.getContentPane().add(BorderLayout.NORTH,panel);
    JPanel panel2 = new JPanel();
    frame.getContentPane().add(BorderLayout.CENTER,panel2);
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();
    JPanel panel5 = new JPanel();
    JPanel panel6 = new JPanel();
    JPanel panel7 = new JPanel();
    panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
    panel2.add(panel3);
    panel2.add(panel4);
    panel2.add(panel5);
    panel2.add(panel6);
    panel2.add(panel7);

    field = new JTextField(12);
    Font inputFont = new Font("serif",Font.BOLD,28);
    field.setFont(inputFont);
    panel.add(field);
    JButton button7 = new JButton("7");
    button7.addActionListener(new button7Listener());
    JButton button8 = new JButton("8");
    button8.addActionListener(new button8Listener());
    JButton button9 = new JButton("9");
    button9.addActionListener(new button9Listener());
    JButton buttonplus = new JButton("+");
    buttonplus.addActionListener(new buttonplusListener());
    panel3.add(button7);
    panel3.add(button8);
    panel3.add(button9);
    panel3.add(buttonplus);
    
    JButton button4 = new JButton("4");
    button4.addActionListener(new button4Listener());
    JButton button5 = new JButton("5");
    button5.addActionListener(new button5Listener());
    JButton button6 = new JButton("6");
    button6.addActionListener(new button6Listener());
    JButton buttonminus = new JButton("-");
    buttonminus.addActionListener(new buttonminusListener());
    panel4.add(button4);
    panel4.add(button5);
    panel4.add(button6);
    panel4.add(buttonminus);
    
    JButton button1 = new JButton("1");
    button1.addActionListener(new button1Listener());
    JButton button2 = new JButton("2");
    button2.addActionListener(new button2Listener());
    JButton button3 = new JButton("3");
    button3.addActionListener(new button3Listener());
    JButton buttontimes = new JButton("x");
    buttontimes.addActionListener(new buttontimesListener());
    panel5.add(button1);
    panel5.add(button2);
    panel5.add(button3);
    panel5.add(buttontimes);
    
    JButton buttondot = new JButton(".");
    buttondot.addActionListener(new buttondotListener());
    JButton button0 = new JButton("0");
    button0.addActionListener(new button0Listener());
    JButton buttonequals = new JButton("=");
    buttonequals.addActionListener(new buttonequalsListener());
    JButton buttondiv = new JButton("/");
    buttondiv.addActionListener(new buttondivideListener());
    panel6.add(buttondot);
    panel6.add(button0);
    panel6.add(buttonequals);
    panel6.add(buttondiv);
    
    JButton buttonclear = new JButton("Clear!");
    buttonclear.setBackground(Color.RED);
    buttonclear.addActionListener(new buttonclearListener());
    panel7.add(buttonclear);
    
}
public static void main(String[] args){
    Calculator cal = new Calculator();
    cal.run();
}
class button1Listener implements ActionListener{
    public void actionPerformed(ActionEvent event){
if(cure==0){
    String c1 = field.getText();
    String c11 = c1+"1";
    field.setText(c11);
}
else{
    field.setText("1");
    cure=0;
}

        
    }
}
class button0Listener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(cure==0){
            String c1 = field.getText();
            String c11 = c1+"0";
            field.setText(c11);
        }
        else{
            field.setText("0");
            cure=0;
        }
    }
}
class buttondotListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(cure==0){
            String c1 = field.getText();
            String c11 = c1+".";
            field.setText(c11);
        }
        else{
            field.setText(".");
            cure=0;
        }
        
    }
}
class button2Listener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(cure==0){
            String c1 = field.getText();
            String c11 = c1+"2";
            field.setText(c11);
        }
        else{
            field.setText("2");
            cure=0;
        }
    }
}
class button3Listener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(cure==0){
            String c1 = field.getText();
            String c11 = c1+"3";
            field.setText(c11);
        }
        else{
            field.setText("3");
            cure=0;
        }
    }
}
class button4Listener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(cure==0){
            String c1 = field.getText();
            String c11 = c1+"4";
            field.setText(c11);
        }
        else{
            field.setText("4");
            cure=0;
        }
    }
}
class button5Listener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(cure==0){
            String c1 = field.getText();
            String c11 = c1+"5";
            field.setText(c11);
        }
        else{
            field.setText("5");
            cure=0;
        }
    }
}
class button6Listener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(cure==0){
            String c1 = field.getText();
            String c11 = c1+"6";
            field.setText(c11);
        }
        else{
            field.setText("6");
            cure=0;
        }
    }
}
class button7Listener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(cure==0){
            String c1 = field.getText();
            String c11 = c1+"7";
            field.setText(c11);
        }
        else{
            field.setText("7");
            cure=0;
        }
    }
}
class button8Listener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(cure==0){
            String c1 = field.getText();
            String c11 = c1+"8";
            field.setText(c11);
        }
        else{
            field.setText("8");
            cure=0;
        }
    }
}
class button9Listener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if(cure==0){
            String c1 = field.getText();
            String c11 = c1+"9";
            field.setText(c11);
        }
        else{
            field.setText("9");
            cure=0;
        }
        
    }
}
class buttonplusListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        plus();
        
    }
}
class buttonminusListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        minus();
        
    }
}
class buttontimesListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        times();
        
    }
}
class buttondivideListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        divide();
        
    }
}
class buttonequalsListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
    equal();
        
    }
}
class buttonclearListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
    field.setText("");
    op=0;
    }
}
public double plus(){
    x ="plus";
    cure=1;
    if(op==0&&!(prev.equals("plus")))
    {
        String val = field.getText();
    double value = Double.parseDouble(val);
    op=value;
    prev="plus";
    return op;
    }
    else if(op!=0&&!(prev.equals("plus"))&&!(prev.equals("times"))&&!(prev.equals("divide"))&&!(prev.equals("minus"))){
        String val = field.getText();
        double value = Double.parseDouble(val);
        op=value;
        prev="plus";
        return op;
        
    }
    else if(op!=0&&prev.equals("plus")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op+value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="plus";
        return op;
    }
    else if(op!=0&&prev.equals("minus")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op-value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="plus";
        return op;
    }
    else if(op!=0&&prev.equals("times")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op*value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="plus";
        return op;
    }
    else if(op!=0&&prev.equals("divide")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op/value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="plus";
        return op;
    }
    
    return op;
    }
public double minus(){
    x ="minus";
    cure=1;
    if(op==0&&!(prev.equals("minus")))
    {
        String val = field.getText();
    double value = Double.parseDouble(val);
    op=value;
    prev="minus";
    return op;
    }
    else if(op!=0&&!(prev.equals("minus"))&&!(prev.equals("times"))&&!(prev.equals("plus"))&&!(prev.equals("divide"))){
        String val = field.getText();
        double value = Double.parseDouble(val);
        op=value;
        prev="minus";
        return op;
        
    }
    else if(op!=0&&prev.equals("minus")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op-value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="minus";
        return op;
    }
    else if(op!=0&&prev.equals("plus")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op+value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="minus";
        return op;
    }
    else if(op!=0&&prev.equals("times")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op*value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="minus";
        return op;
    }
    else if(op!=0&&prev.equals("divide")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op/value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="minus";
        return op;
    }
    
    return op;
    }

public double times(){
    x ="times";
    cure=1;
    if(op==0&&!(prev.equals("times")))
    {
        String val = field.getText();
    double value = Double.parseDouble(val);
    op=value;
    prev="times";
    return op;
    }
    else if(op!=0&&!(prev.equals("times"))&&!(prev.equals("divide"))&&!(prev.equals("plus"))&&!(prev.equals("minus"))){
        String val = field.getText();
        double value = Double.parseDouble(val);
        op=value;
        prev="times";
        return op;
        
    }
    else if(op!=0&&prev.equals("times")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op*value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="times";
        return op;
    }
    else if(op!=0&&prev.equals("minus")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op-value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="times";
        return op;
    }
    else if(op!=0&&prev.equals("plus")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op+value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="times";
        return op;
    }
    else if(op!=0&&prev.equals("divide")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op/value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="times";
        return op;
    }
    
    return op;
    
    }

public double divide(){
    x ="divide";
    cure=1;
    if(op==0&&!(prev.equals("divide")))
    {
        String val = field.getText();
    double value = Double.parseDouble(val);
    op=value;
    prev="divide";
    return op;
    }
    else if(op!=0&&!(prev.equals("divide"))&&!(prev.equals("times"))&&!(prev.equals("plus"))&&!(prev.equals("minus"))){
        String val = field.getText();
        double value = Double.parseDouble(val);
        op=value;
        prev="divide";
        return op;
        
    }
    else if(op!=0&&prev.equals("divide")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op/value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="divide";
        return op;
    }
    else if(op!=0&&prev.equals("minus")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op-value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="divide";
        return op;
    }
    else if(op!=0&&prev.equals("times")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op*value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="divide";
        return op;
    }
    else if(op!=0&&prev.equals("plus")) {
        String val = field.getText();    
        double value = Double.parseDouble(val);
        op=op+value;
        Double q1 = op;
        String q2 = q1.toString();
        field.setText(q2);
        prev="divide";
        return op;
    }
    
    return op;
    }

public void equal(){
    switch(x){
    case "plus" :
    String eq =    field.getText();
    double equal = Double.parseDouble(eq);
    Double result = op + equal;
    String res = result.toString();
    field.setText(res);
    x="null";
    break;
    
    case "minus" :
        String eq2 =    field.getText();
        double equal2 = Double.parseDouble(eq2);
        Double result2 = op - equal2;
        String res2 = result2.toString();
        field.setText(res2);
        x="null";
    break;
    
    case "times" :
        String eq3 =    field.getText();
        double equal3 = Double.parseDouble(eq3);
        Double result3 = op*equal3;
        String res3 = result3.toString();
        field.setText(res3);
        x="null";
    break;
    
    case "divide" :
        String eq4 =    field.getText();
        double equal4 = Double.parseDouble(eq4);
        Double result4 = op/equal4;
        String res4 = result4.toString();
        field.setText(res4);
        x="null";
    break;
    
    }    
    prev="null";
}
}

21. By Kamlesh Kumar

Made by Kamlesh Kumar. Enter the first number then the second number, after that choose the operator you want to use. ( Source )

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

    	double num1, num2;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter first number:");

        /* We are using data type double so that user
         * can enter integer as well as floating point
         * value
         */
        num1 = scanner.nextDouble();
        System.out.print("Enter second number:");
        num2 = scanner.nextDouble();

        System.out.print("Enter an operator (+, -, *, /): ");
        char operator = scanner.next().charAt(0);

        scanner.close();
        double output;

        switch(operator)
        {
            case '+':
            	output = num1 + num2;
                break;

            case '-':
            	output = num1 - num2;
                break;

            case '*':
            	output = num1 * num2;
                break;

            case '/':
            	output = num1 / num2;
                break;

            /* If user enters any other operator or char apart from
             * +, -, * and /, then display an error message to user
             * 
             */
            default:
                System.out.printf("You have entered wrong operator");
                return;
        }

        System.out.println(num1+" "+operator+" "+num2+": "+output);
    }
}

22. By Paul Yan

Made by Paul Yan. You have to enter the calculation in different lines, first enter the first number then the operator you want to use, after that the second number. ( Source )

import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
    
    Scanner scan = new Scanner(System.in);
        double num1 = scan.nextDouble(); 
        String operation = scan.next();
        double num2 = scan.nextDouble();
        
    switch (operation) {
    
        case "+" : 
        System.out.println(num1 + num2);
        break;
        
        case "-" : 
        System.out.println(num1 - num2);
        break;
        
        case "*" : 
        System.out.println(num1 * num2);
        break;
        
        case "/" : 
        System.out.println(num1 / num2);
        break;
        
        default :
        System.out.println("NOT AN OPERATION");
        return;
    }
    scan.close();
 }
}

23. By Shobh

Made by Shobh. You have to enter the calculation in different lines, first enter the two numbers in separate lines after that enter what operator you want to use – add, subtract, multiply, divide. ( Source )

import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int a = s.nextInt();
    int b = s.nextInt();
    String c = s.next();
       {
     if (c.equalsIgnoreCase ("add"))
     System.out.println(a+b);
     else if (c.equalsIgnoreCase("subtract"))
     System.out.println(a-b);
     else if (c.equalsIgnoreCase("multiply"))
     System.out.println(a*b);
     else if(c.equalsIgnoreCase ("divide"))
     System.out.println(a/b);
     else
     System.out.println("error");
       }
     }
}

24. By Aryan

Multiplication java calculator by Aryan. It can only do multiplication, enter first and second number for output. ( Source )

import java.util.Scanner; // importing the Scanner class 

public class Program {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);
    System.out.println("Mutiplication calculator");
    System.out.println("Enter first number-"); 
    int a = myObj.nextInt();
    System.out.println("Enter second number");
    int b = myObj.nextInt();
    int product = a*b;
    System.out.println("Your answer is:-");
    System.out.println(product);
    
   System.out.println("Addition calculator");
   System.out.println("Enter first number-");
   int x = myObj.nextInt();
   System.out.println("Enter second number");
   int y = myObj.nextInt();
   int sum = x+y;
   System.out.println("Your answer is:-");
   System.out.println(sum);
   
   System.out.println("HOW WAS MY FIRST JAVA PROJECT?");
   String input;
   input = myObj.nextLine();
   System.out.println("input");
   System.out.println("Thank you very muchπŸ˜€");
   
   //My project gets finished up here 
   //upvote if you enjoyed 😊it
   //Thanks
   
     
  }
}

25. By Dalip Kumar

Java calculator by Dalip Kumar. Enter the first number in first line, after that the operator in second line, after that the second number in third line. ( Source )

import java.util.Scanner ;
class MyGratitude{
    public static void main(String [] args){
    Scanner sc=new Scanner (System.in);
   
            System.out.println ("First Value");
     int x=sc.nextInt();
     
     
      System.out.println ("Operation");
      sc.nextLine();
 char  operation=sc.nextLine().charAt(0);       
 
 System.out.println("Second Value");
     int y=sc.nextInt();
    
    int result=0;
switch (operation ){
    case '+':result=x+y;
    break ;
    case '-':result=x-y;
    break ;
    case '/':result=x/y;
    break ;
    case '*':result=x*y;
    break;
    default :
    System.out.println("Invalid operation ");
    }
    System.out.println("the result is " + result );
    

    }
}

26. By Kafeelur Rahman T K

Made by Kafeelur Rahman T K. Enter number – operation – number. ( Source )

################################
Enter a value
Choose anyone
+
-
*
/
s -Square root
Enter x to Exit
Enter Second value
10.0+20.0 = 30.0

################################
Thank you For using it  #
################################
import java.util.Scanner;
public class Kafeel
{
   public static void main(String[] args) throws Exception  {
    try
    {
    float a,b;
    Scanner s=new Scanner(System.in);
    char ch;
    System.out.println("\t\t\[email protected]@@@ Calculator @@@@");
    do
    {
        System.out.println("################################");
        System.out.println("Enter a value");
        a=s.nextFloat();       
        System.out.println("Choose anyone");
        System.out.println("+");
        System.out.println("-");
        System.out.println("*");
        System.out.println("/");
        System.out.println("s -Square root");
        System.out.println("Enter x to Exit");
        ch=s.next().charAt(0);
        if(ch=='x')
        {
            System.out.println("Thank You");
            System.exit(0);
         }
        if(ch=='s')
        {
          if(a<0)
          {
            double sq=a*(-1);
            sq=Math.sqrt(sq);
            System.out.println("sqrt("+a+") = i"+sq);
          }
          else
          {
          double sq=Math.sqrt(a);
          System.out.println("SQRT of "+a+" ="+sq);
          }
        }
        if(ch !='s')
        {
        System.out.println("Enter Second value");
        b=s.nextFloat();
        if(ch=='+')
        {
          float   sum=a+b;
          System.out.print(a+"+"+b);
          System.out.println(" = "+sum);
          System.out.println();
        }
        else if(ch=='-')
        {
            float diff=a-b;
            System.out.print(a+"-"+b);
            System.out.println(" = "+diff);
            System.out.println();
        }
        else if(ch=='*')
        {
            float  mul=a*b;
            System.out.print(a+"*"+b);
            System.out.print(" = "+mul);
            System.out.println();
        }
        else if(ch=='/')
        {         
            float div=a/b;
            System.out.print(a+"/"+b);
            System.out.println(" = "+div);
            System.out.println();
        }
        else
           System.out.println("Invalid Input");
       } System.out.println("################################");
        System.out.println("Thank you For using it  #");
    }while(ch !='x');
     }
     
     catch(Exception e)   
     {
         System.out.println(e.getMessage());
     }
    }
}

27. By Heng Jun Xi

Made by Heng Jun Xi. ( Source )

Problem : Formula : 1+1
Answer : 2.0
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;

public class Calculator{
    
    static Pattern pattern;
    static Matcher matcher;
    static String unsignedInteger = "([\\d]+)";
    static String signedInteger = "([+-]*"+unsignedInteger+")";
    static String decimal = "(\\.[\\d]*)?";
    static String exponent = "(E"+signedInteger+decimal+")?";
    static String unsignedNumber = unsignedInteger+decimal+exponent;
    static String signedNumber = signedInteger+decimal+exponent;
    static String operands = "[\\Q+-*/%^\\E]";
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.print("Problem : ");
        String formula = sc.nextLine();
        System.out.print("Formula : ");
        System.out.println(formula.replaceAll("\\s", ""));
        System.out.print("Answer : ");
        System.out.println(calc(formula));
        sc.close();
    }
    
    static String calc(String formula){
        formula = formula.replaceAll("\\s", "");
        formula = calcSign(formula);
        if(!formula.matches("("+signedNumber+operands+")*"+signedNumber))
            return "Syntax Error!";
        
        while(formula.contains("^")){
            formula = calc(formula,'^');
        }
        for(int i = 1; i < formula.length(); i++){
            switch(formula.charAt(i)){
            case '*':
                formula = calc(formula,'*');
                i = 1;
                break;
            case '/':
                formula = calc(formula,'/');
                i = 1;
                break;
            case '%':
                formula = calc(formula,'%');
                i = 1;
                break;
            }
        }
        for(int i = 1; i < formula.length(); i++){
            switch(formula.charAt(i)){
            case '+':
                formula = calc(formula,'+');
                i = 1;
                break;
            case '-':
                formula = calc(formula,'-');
                i = 1;
                break;
            }
        }
        return formula;
    }
    
    static String calcSign(String formula){
        for(int i = 1; i < formula.length(); i++){
            
            if(formula.charAt(i)=='-'){
                if(formula.charAt(i-1)=='-'){
                    formula = formula.replace("--", "+");
                    i = 1;
                }
                else if(formula.charAt(i-1)=='+'){
                    formula = formula.replace("+-", "-");
                    i = 1;
                }
            } if(formula.charAt(i)=='+'){
                if(formula.charAt(i-1)=='+'){
                    formula = formula.replace("++", "+");
                    i = 1;
                }
                else if(formula.charAt(i-1)=='-'){
                    formula = formula.replace("-+", "-");
                    i = 1;
                }
            }
        }
        while(formula.charAt(0)=='+'){
            formula = formula.substring(1);
        }
        return formula;
    }
    
    static String calc(String formula,char c){
        if(c=='^'||c=='*'||c=='/'||c=='%')
            pattern = Pattern.compile(unsignedNumber+"\\"+c+signedNumber);
        else if(c=='+'||c=='-')
            pattern = Pattern.compile(signedNumber+"\\"+c+unsignedNumber);
        matcher = pattern.matcher(formula);
        if(matcher.find()){
            String[] arr;
            if(matcher.group().startsWith("-")){
                arr = matcher.group().substring(1).split("\\"+c,2);
                arr[0] = "-"+arr[0];
            }
            else    
                arr = matcher.group().split("\\"+c,2);
            switch(c){
            case '^':
                formula = formula.replace(matcher.group(), Double.toString(Math.pow(Double.parseDouble(arr[0]), Double.parseDouble(arr[1]))));
                break;
            case '*':
                formula = formula.replace(matcher.group(), Double.toString(Double.parseDouble(arr[0])*Double.parseDouble(arr[1])));
                break;
            case '/':
                try{
                    if(Double.parseDouble(arr[1])==0.0) throw new ArithmeticException();
                }catch(ArithmeticException ex){
                    return "A number cannot be divided by zero!";
                }
                formula = formula.replace(matcher.group(), Double.toString(Double.parseDouble(arr[0])/Double.parseDouble(arr[1])));
                break;
            case '%':
                try{
                    if(Double.parseDouble(arr[1])==0.0) throw new ArithmeticException();
                }catch(ArithmeticException ex){
                    return "A number cannot be divided by zero!";
                }
                formula = formula.replace(matcher.group(), Double.toString(Double.parseDouble(arr[0])%Double.parseDouble(arr[1])));
                break;    
            case '+':
                formula = formula.replace(matcher.group(), Double.toString(Double.parseDouble(arr[0])+Double.parseDouble(arr[1])));
                break;
            case '-':
                formula = formula.replace(matcher.group(), Double.toString(Double.parseDouble(arr[0])-Double.parseDouble(arr[1])));
                break;
            }
            formula = calcSign(formula);
        }
        return formula;
    }
}

28. By Shatmanliev T

Made by Shatmanliev T. Enter first and second number to get its addition. ( Source )

enter first number 1
enter second number 1
2.0
import java.util.Scanner;

class MyClass {
    public static void main(String[ ] args) {
        Scanner myVar = new Scanner(System.in);
        double fnum,snum,answer;
        System.out.println("enter first number");
        fnum=myVar.nextDouble();
        System.out.println("enter second number");
        snum=myVar.nextDouble();
        answer=fnum+snum;
        System.out.println(answer);
    }
}

29. By MohammedSissa.Eng

Made by MohammedSissa.Eng. You should enter 2 numbers then choose the “choice” when you still inputting the numbers (all inputs at first )” Ex : if i entered at first 12 2 3 it will return 24… that mean that 12 is first number … 2 is second number … and 3 is the choice. ( Source )

1)sum
2)sub
3)multi
4)divi
5)power
Sum is 10.0

import java.util.Scanner ;  
public class Program
{
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        double a=0,b=0;
          System.out.println("1)sum");
        System.out.println("2)sub");
        System.out.println("3)multi");
        System.out .println("4)divi");
        System.out.println("5)power");
        a = s.nextDouble();
        b = s.nextDouble();
        int choice =0;
      
        choice = s.nextInt();
        switch(choice){
            case 1: System.out.println("Sum is "+ sum(a,b));break;
            case 2:
            System.out.println("sub is " +sub(a,b));break;
            case 3:
            System.out.println( "multi is" +multi(a,b));break;
            case 4:
            System.out.println( "division is " +divi(a,b));break;
            default :
            System.out.println("Error!!");break;
            case 5:
            System.out.println( "power is " +power(a,b));break;
        }
    }
    static double  sum(double  x, double  y){
        return x+y;
    }
    static double  sub(double  x,double  y){
        return x-y;
    }
    static double  divi(double x,double  y){
        return x/y;
    }
    static double  multi(double  x,double  y){
        return x*y;
    }
    static double power(double x,double y){
        int p =1;
        for (int i=1;i<=y;i++){
            p*=x;
        }
        return p;
    }
}

30. By Ujjwal Kumar

Made by Ujjwal Kumar. Simple Java Calculator. ( Source )

first number =4.0
second number=20.0
addition of numbers=24.0
first number =7.29
second number=18.83
multiplication of numbers is=137.27069999999998
class calculator{
public static void calculator(double n1,double n2, char chr){
double result = 0.0;{
System.out.println("first number =" +n1);
System.out.println("second number=" +n2);
}
switch(chr){
case '+' : result =n1+n2;
System.out.println("addition of numbers=" +result);
break;
case '_' : result =n1-n2;
System.out.print("substraction of numbers is=" +result);
break;
case '*' : result = n1*n2;
System.out.print("multiplication of numbers is=" +result);
break;
case '/' : result = n1/n2;
System.out.print("result of division is="+ result );
break;
case '%' : result = n1%n2;
System.out.print("result of modulus operator is =" +result);
break;
default: System.out.println("operator selected is wrong");
}
}
public static void main(String args[]){
    calculator(4.0d, 20.0d, '+');
    calculator(7.29d, 18.83d, '*');
}
}

31. By Alireza Shirzad

Made by Alireza Shirzad. ( Source )

Number 2
Operator +
Number 2
4
import java.util.Scanner;

public class Program
{
//Alireza Shzd
static int x,z ;

    public static void main(String[] args) {
    
    Scanner scn = new Scanner(System.in);
    
    System.out.println("Number");
    
    x = scn.nextInt();
    
    System.out.println("Operator");
    
    String in = scn.next();
    
    System.out.println("Number");
    
    z = scn.nextInt();
    
    switch (in){
        
        //total
        case "+":
        System.out.println(x + z);
        break;
        
        //minus
        case "-":
        System.out.println(x - z);
        break;
        
        //Multiplication
        case "Γ—":
        System.out.println(x * z);
        break;
        
        //division
        case "Γ·":
        System.out.println(x / z);
        break;
    }
        
    }
}

32. By venkatesh savalgi

Made by venkatesh savalgi. ( Source )

total is -129500
total is 360
total is -160
total is 360
total is 200
total is 460
total is 129600
total is 820
total is 100500
public class Program
{
    public static void main(String[] args) {
     
    int a=100;
    int b=260;
    int c=a+b;
    int e=a-b;
    int f=b+a;
    int g=c+e;
    int h=c*f;
    int i=a+c;
    int j=a-h;
    
    int k=c+i;
    int l=15500;
    int m=85000;
    int n=l+m;
    System.out.println ("total is "+j);
    
System.out.println ("total is "+c);

System.out.println ("total is "+e);


System.out.println ("total is "+f);
System.out.println ("total is "+g);

System.out.println ("total is "+i);
System.out.println ("total is "+h);
System.out.println ("total is "+k);
System.out.println ("total is "+n);

    }
       
}

33. By Name

Made by Name. ( Source )

3
+
3
6
import java.util.Scanner;
public class Program
{
    static int calc1(int n1, int n2){
        return n1 + n2;
    }
    static int calc2(int n1, int n2){
        return n1 - n2;
    }
    static int calc3(int n1, int n2){
        return n1 * n2;
    }
    static double clac4(float n1, float n2){
        return n1 / n2;
    }
    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        int fint = inp.nextInt();
        String sign = inp.next();
        int sint = inp.nextInt();
        //System.out.println(fint);
        //System.out.println(sign);
        //System.out.println(sint);
        if (sign.equals("+")){
            System.out.println(calc1(fint, sint));
        }
        else if(sign.equals("-")){
            System.out.println(calc2(fint, sint));
        }
        else if(sign.equals("*")){
            System.out.println(calc3(fint, sint));
        }
        else if(sign.equals("/")){
            System.out.println(clac4(fint, sint));
        }
    }
}