Basic Calculator Program Using Java Last Updated : 22 May, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user input. Example: Enter the numbers: 2 2 Enter the operator (+,-,*,/) + The final result: 2.0 + 2.0 = 4.0ApproachTake two numbers using the Scanner class. The switch case branching is used to execute a particular section.Using a switch case to evaluate respective operations. Below is the Java program to implement the calculator: Java // Java program for simple calculator import java.io.*; import java.lang.*; import java.lang.Math; import java.util.Scanner; // Driver class public class BasicCalculator { // main function public static void main(String[] args) { // Stores two numbers double num1, num2; // Take input from the user Scanner sc = new Scanner(System.in); System.out.println("Enter the numbers:"); // Take the inputs num1 = sc.nextDouble(); num2 = sc.nextDouble(); System.out.println("Enter the operator (+,-,*,/):"); char op = sc.next().charAt(0); double o = 0; switch (op) { // case to add two numbers case '+': o = num1 + num2; break; // case to subtract two numbers case '-': o = num1 - num2; break; // case to multiply two numbers case '*': o = num1 * num2; break; // case to divide two numbers case '/': o = num1 / num2; break; default: System.out.println("You enter wrong input"); } System.out.println("The final result:"); System.out.println(); // print the final result System.out.println(num1 + " " + op + " " + num2 + " = " + o); } } Output: Enter the numbers: 2 2 Enter the operator (+,-,*,/) + The final result: 2.0 + 2.0 = 4.0 Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Basic Calculator Program Using Java S sdpapu Follow Improve Article Tags : Java java-basics Practice Tags : Java Similar Reads Java Programming Basics Java is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming console 4 min read Java Swing | Simple Calculator Java Swing is a GUI (graphical user Interface) widget toolkit for Java. Java Swing is a part of Oracle's Java foundation classes . Java Swing is an API for providing graphical user interface elements to Java Programs.Swing was created to provide more powerful and flexible components than Java AWT (A 4 min read Simple Calculator using TCP in Java Prerequisite: Socket Programming in Java Networking just doesn't conclude with a one-way communication between the client and server. For example consider a time telling server which listens to request of the clients and respond with the current time to the client. Real-time applications usually fol 5 min read Java Program to Convert Currency using AWT Swing is a part of the JFC (Java Foundation Classes). Building a Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components that allow a high level of customization and provide rich functionalities and is used to create window-based applications. 4 min read Student Grade Calculator using Java Swing Consider the following scenario. We need to create a GUI based application which calculates grade of students according to marks obtained in 4 subjects. Below are the range of marks for different grades. MarksGrade>90%ABetween 85% - 90%BBetween 80% - 85%CBetween 70% - 80%DBetween 60% - 70%EBetwee 4 min read Like