Register Now

Login


Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Simple JAVA Calculator

Here is a simple JAVA Calculator program that is able to add, subtract, multiply or divide two numbers. It uses a switch case to perform the specified operation on the two numbers. If the operator entered is incorrect, an error message is displayed. The code snippet that demonstrates this is given as follows:

Simple JAVA Calculator Source code:

Here is an example of a simple calculator program written in Java:

import java.util.Scanner;

public class Calculator {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter a number: ");
    double num1 = input.nextDouble();

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

    System.out.print("Enter another number: ");
    double num2 = input.nextDouble();

    double result;

    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("Invalid operator");
        return;
    }

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


This program prompts the user to enter two numbers and an operator, and then performs the specified operation on the numbers. The result is printed to the console. The program uses a switch statement to determine which operation to perform based on the operator entered by the user.

A Java calculator is a software application that performs basic arithmetic operations, such as addition, subtraction, multiplication, and division. It may also support more advanced operations, such as square roots, logarithms, and trigonometric functions.

A Java calculator may be implemented as a standalone program or as a component of a larger application. It typically includes a user interface that allows the user to enter numbers and choose an operation, and a back-end component that performs the calculations and displays the results. The calculator may be designed to work with real numbers or with other numeric data types, such as integers or fractions.

Java is a popular programming language that is widely used for creating applications of all kinds, including calculators. It is known for its object-oriented design, portability, and security features, which make it well-suited for building robust and scalable software applications.

Comment ( 1 )

  1. ah can you make a simple java application for me ?

Leave a reply