simple calculator java source code code example

Example 1: how to make a calculator in java

// this was all coded by me, to use it get Eclipse, or IntelliJ and make a new
// Java Project

package javaCode;

import java.util.Scanner;

public class testCode {

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Would you like to Add, Subtract, Divide, or Multiply?");
		
		String mathType = scan.next();
		
		if (mathType.equalsIgnoreCase("add")) {
			System.out.println("Whats the first number you want to add?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " + ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " + " + secondNumber + " =");
			System.out.println(firstNumber + secondNumber);
			
		} else if (mathType.equalsIgnoreCase("subtract")) {
			System.out.println("Whats the first number you want to subtract?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " - ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " - " + secondNumber + " =");
			System.out.println(firstNumber - secondNumber);
		} else if (mathType.equalsIgnoreCase("divide")) {
			System.out.println("Whats the first number you want to divide?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " % ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " % " + secondNumber + " =");
			System.out.println(firstNumber / secondNumber);
		} else if (mathType.equalsIgnoreCase("multiply")){
			System.out.println("Whats the first number you want to multiply?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " x ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " x " + secondNumber + " =");
			System.out.println(firstNumber * secondNumber);
		}
		
	}

}

Example 2: simple calculator program in java

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        System.out.print("Enter two numbers: ");

        // nextDouble() reads the next double from the keyboard
        double first = reader.nextDouble();
        double second = reader.nextDouble();

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

        double result;

        switch(operator)
        {
            case '+':
                result = first + second;
                break;

            case '-':
                result = first - second;
                break;

            case '*':
                result = first * second;
                break;

            case '/':
                result = first / second;
                break;

            // operator doesn't match any case constant (+, -, *, /)
            default:
                System.out.printf("Error! operator is not correct");
                return;
        }

        System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
    }
}

Tags:

Java Example