Simple Calculator Using Java With Source Code

Simple Calculator Using Java​

Introduction:

A simple calculator is a type of electronic calculator that is designed to perform mathematical operations, such as addition,subtraction,multiplication,division. These calculators are commonly used in fields such as engineering, physics, and finance. In this article, we will be discussing a basic scientific calculator program written in Java.

Explanation:

The program begins by importing the Scanner class, which is used to take user input. The program then prompts the user to enter the first number, the second number, and the desired mathematical operation. The user input is stored in variables num1, num2, and operator respectively. A switch statement is then used to determine which operation to perform based on the value of the operator variable.

The program supports the following mathematical operations:

  • Addition, represented by the “+” operator
  • Subtraction, represented by the “-” operator
  • Multiplication, represented by the “*” operator
  • Division, represented by the “/” operator
  • Exponentiation, represented by the “^” operator

If the operator entered is not one of the above, the program will print “Invalid operator entered” and exit.

The program then performs the appropriate calculation and stores the result in the variable “result” . For example, if the operator is “+”, the program will add num1 and num2, and if the operator is “^”, program will use Math.pow(num1,num2) method to calculate the result. Finally, the program prints the result to the console for the user to see.

This program provides a simple and user-friendly interface for performing common scientific calculations. It can be a good starting point for building more advanced calculators with additional features and functionality. With this program, you can easily perform various mathematical operations and get the result in no time. This program can be useful in fields such as engineering, physics, and finance where performing mathematical operations is a common task.

Source Code:

				
					import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the first number: ");
        double num1 = input.nextDouble();
        System.out.println("Enter the second number: ");
        double num2 = input.nextDouble();
        System.out.println("Enter the operation you would like to perform (+, -, *, /, ^): ");
        String operator = input.next();
        double result;
        
        switch(operator) {
            case "+":
                result = num1 + num2;
                System.out.println("The result is: " + result);
                break;
            case "-":
                result = num1 - num2;
                System.out.println("The result is: " + result);
                break;
            case "*":
                result = num1 * num2;
                System.out.println("The result is: " + result);
                break;
            case "/":
                result = num1 / num2;
                System.out.println("The result is: " + result);
                break;
            case "^":
                result = Math.pow(num1, num2);
                System.out.println("The result is: " + result);
                break;
            default:
                System.out.println("Invalid operator entered");
                break;
        }
    }
}
				
			

It’s worth to mention that this program is a basic calculator and it can be further improved by adding more functionalities, error handling, GUI etc.

In summary, this basic scientific calculator program written in Java is a great tool for performing common mathematical operations quickly and easily. It abstracts the complexity of mathematical operations and provides a user-friendly interface. With this program, you can perform various mathematical operations and get the result without any hassle.

Output:

Simple calculator using java

Find More Projects

Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …

Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …

Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …

Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …

Digital and Analog Clock using HTML CSS and JavaScript Introduction : This project is a digital clock and stopwatch system, which allows …

Coffee Shop Website using HTML, CSS & JavaScript Introduction : This project is a website for coffee house business. It uses HTML …

All Coding Handwritten Notes