Calculator in Java with Source Code

Sure, here’s a simple calculator program written in Java with source code:

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double num1, num2, result;
        char operator;
        
        System.out.println("Enter first number: ");
        num1 = input.nextDouble();
        
        System.out.println("Enter second number: ");
        num2 = input.nextDouble();
        
        System.out.println("Enter operator (+, -, *, /): ");
        operator = input.next().charAt(0);
        
        switch(operator) {
            case '+':
                result = num1 + num2;
                System.out.println(num1 + " + " + num2 + " = " + result);
                break;
                
            case '-':
                result = num1 - num2;
                System.out.println(num1 + " - " + num2 + " = " + result);
                break;
                
            case '*':
                result = num1 * num2;
                System.out.println(num1 + " * " + num2 + " = " + result);
                break;
                
            case '/':
                if(num2 == 0) {
                    System.out.println("Error: Division by zero");
                } else {
                    result = num1 / num2;
                    System.out.println(num1 + " / " + num2 + " = " + result);
                }
                break;
                
            default:
                System.out.println("Error: Invalid operator");
                break;
        }
    }
}

This calculator program asks the user to input two numbers and an operator (+, -, *, /) to perform the desired operation. It then uses a switch statement to perform the operation and print the result.

To run the program, simply compile and execute the Calculator class. You can do this by opening a command prompt or terminal, navigating to the directory where the Calculator.java file is saved, and entering the following commands:

javac Calculator.java
java Calculator

This will compile and execute the program, allowing you to use the calculator.