Simple calculator using java

Simple Calculator Using Java​

Abstract:

This program is a Java implementation of a basic scientific calculator that allows the user to perform basic mathematical operations such as addition, subtraction, multiplication, division and exponentiation. The program prompts the user to input two numbers and a desired operation, and then uses a switch statement to perform the appropriate calculation and return the result. The program abstracts the complexity of mathematical operations and provides a simple and user-friendly interface for performing common calculations. This program can be a good starting point for building more advanced calculators with additional features and functionality.

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

Hostel Management System Using Python With Source Code by using Python Graphical User Interface GUI Introduction: Managing a hostel efficiently can be …

Contra Game Using Python with source code using Pygame Introduction: Remember the super fun Contra game where you fight aliens and jump …

Restaurant Billing Management System Using Python with Tkinter (Graphical User Interface) Introduction: In the bustling world of restaurants, efficiency is paramount, especially …

Jungle Dash Game Using Python with source code with pygame module Introduction: Dive into the excitement of jungle adventures with our new …

Building a Tetris Game with Python with source code Introduction: Welcome to the world of Tetris, where falling blocks meet strategic maneuvers …

Super Mario Game Using Python with source code Introduction: Remember playing Super Mario as a kid? It’s that classic game where you …

All Coding Handwritten Notes

Browse Handwritten Notes