
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Basic Calculator Program Using Java
In this article, we will learn to create a basic calculator using Java. With a basic calculator, we can add, subtract, multiply, or divide two numbers. This is done using a switch case. A program that demonstrates this is given as follows ?
Problem Statement
Write a program in Java to create a basic calculator for performing the basic arithmetic operations ?
Input
Enter two numbers: 2
3
Enter an operator (+, -, *, /): +
Output
The result is given as follows:
2.0 + 3.0 = 5.0
Steps to create basic calculator
Following are the steps to create a basic calculator program using Java ?
- Start by importing the Scanner class from java.util package.
- Initialize the Calculator class.
- In the main method, declare variables for the two numbers (num1, num2), the result (ans), and the operator (op).
- We will use the Scanner to read two numbers and an operator from the user.
- We will use a switch statement to perform the selected arithmetic operation based on the input operator.
- Print the result of the calculation.
Basic calculator program using Java
Below is an example to create a basic calculator using Java ?
import java.util.Scanner; public class Calculator { public static void main(String[] args) { double num1; double num2; double ans; char op; Scanner reader = new Scanner(System.in); System.out.print("Enter two numbers: "); num1 = reader.nextDouble(); num2 = reader.nextDouble(); System.out.print("\nEnter an operator (+, -, *, /): "); op = reader.next().charAt(0); switch(op) { case '+': ans = num1 + num2; break; case '-': ans = num1 - num2; break; case '*': ans = num1 * num2; break; case '/': ans = num1 / num2; break; default: System.out.printf("Error! Enter correct operator"); return; } System.out.print("\nThe result is given as follows:\n"); System.out.printf(num1 + " " + op + " " + num2 + " = " + ans); } }
Output
Enter two numbers: 10.0 7.0 Enter an operator (+, -, *, /): - The result is given as follows: 10.0 - 7.0 = 3.0
Code Explanation
Now let us understand the above program.
The two numbers as well as the operator are acquired from the user. The code snippet that demonstrates this is given as follows ?
double num1; double num2; double ans; char op; Scanner reader = new Scanner(System.in); System.out.print("Enter two numbers: "); num1 = reader.nextDouble(); num2 = reader.nextDouble(); System.out.print("\nEnter an operator (+, -, *, /): "); op = reader.next().charAt(0);
A switch case is used 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 ?
switch(op) { case '+': ans = num1 + num2; break; case '-': ans = num1 - num2; break; case '*': ans = num1 * num2; break; case '/': ans = num1 / num2; break; default: System.out.printf("Error! Enter correct operator"); return; }
Finally, the result is printed. The code snippet that demonstrates this is given as follows ?
System.out.print("\nThe result is given as follows:\n"); System.out.printf(num1 + " " + op + " " + num2 + " = " + ans);