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:

Get Discount on Top Educational Courses

Brand NameDiscount InformationCoupon Codes Link
Educative.io20% discount on Educative courses and plans
W3Schools20% discount on W3Schools courses
KodeKloud10% discount on KodeKloud courses and plans
GeeksforGeeks30% discount on GeeksforGeeks courses
Target Test Prep20% discount on Target Test Prep
Coding Ninjas₹5000 discount on Coding Ninjas courses
Skillshare40% discount on Skillshare
DataCamp50% discount on DataCamp
365 Data Science57% discount on 365 Data Science Plans
Get SmarterFlat 20% discount on Get Smarter courses
SmartKeedaFlat 40% discount on SmartKeeda courses
StackSocial20% discount on StackSocial courses
				
					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

Drawing Chhatrapati Shivaji Maharaj Using Python Chhatrapati Shivaji Maharaj, the legendary Maratha warrior and founder of the Maratha Empire, is an inspiration …

Resume Builder Application using Java With Source Code Graphical User Interface [GUI] Introduction: The Resume Builder Application is a powerful and user-friendly …

Encryption Tool using java with complete source Code GUI Introduction: The Encryption Tool is a Java-based GUI application designed to help users …

Movie Ticket Booking System using Java With Source Code Graphical User Interface [GUI] Introduction: The Movie Ticket Booking System is a Java …

Video Call Website Using HTML, CSS, and JavaScript (Source Code) Introduction Hello friends, welcome to today’s new blog post. Today we have …

promise day using html CSS and JavaScript Introduction Hello all my developers friends my name is Gautam and everyone is welcome to …