Simple calculator program in java using switch case: Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Program to Make a Simple Calculator Using switch-case
Java programs calculator: In this article we will make simple calculator using switch case
statement.
Let’s first see the approach of the program.
Approach:
- First take two numbers input from the user means two operands, upon which the calculation will be performed.
- Store two numbers in double variables
operand1
andoperand2
respectively. - After taking two operands input now take the operator input from the user, means which operation will be performed.
- As operator input the user has to give either one operator among
+
(addition),-
(subtraction),*
(multiplication),/
(division). And store it in character variableoperator
. - Now according to the case(operator symbols) perform the respective operation.
- Then produce the result as output and with the help of double variable
result
.
Let’s see the below program to understand it more clearly.
Program:
import java.util.Scanner; class Main { public static void main(String[] args) { char operator; Double operand1, operand2, result; // Scanner class object created, for user input Scanner sc= new Scanner(System.in); // enter first number System.out.println("Enter first number : "); operand1 = sc.nextDouble(); // enter second number System.out.println("Enter second number : "); operand2 = sc.nextDouble(); // asking for operator user input System.out.println("Choose an operator : +, -, *, or /"); operator = sc.next().charAt(0); switch (operator) { // addition operation case '+': result = operand1+operand2; System.out.println("Result = "+operand1+ " + " +operand2+ " = " + result); break; // subtraction operation case '-': result = operand1-operand2; System.out.println("Result = "+operand1+ " - " +operand2+ " = " + result); break; // multiplication operation case '*': result = operand1*operand2; System.out.println("Result = "+operand1+ " * " +operand2+ " = " + result); break; // division operation case '/': result = operand1/operand2; System.out.println("Result = "+operand1+ " / " +operand2+ " = " + result); break; default: System.out.println("You have entered invalid operator."); break; } System.out.println("THANKS FOR USING BTECHGEEKS CALCULATOR"); sc.close(); } }
Output: CASE-1 ADDITION Enter first number : 605.4 Enter second number : 200 Choose an operator : +, -, *, or / + Result = 605.4 + 200.0 = 805.4 THANKS FOR USING BTECHGEEKS CALCULATOR CASE-2 SUBTRACTION Enter first number : 6789 Enter second number : 2400 Choose an operator : +, -, *, or / - Result = 6789.0 - 2400.0 = 4389.0 THANKS FOR USING BTECHGEEKS CALCULATOR CASE-3 MULTIPLICATION Enter first number : 350 Enter second number : 30 Choose an operator : +, -, *, or / * Result = 350.0 * 30.0 = 10500.0 THANKS FOR USING BTECHGEEKS CALCULATOR CASE-4 DIVISION Enter first number : 24430 Enter second number : 30 Choose an operator : +, -, *, or / / Result = 24430.0 / 30.0 = 814.3333333333334 THANKS FOR USING BTECHGEEKS CALCULATOR
Understand the Programming Language Java and learn the language fastly by using our wide range of Java Programming Examples with Output and try to write programs on your own.
Related Java Decision Making and Loop Programs :
- Java Program to Check Leap Year
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Calculate the Sum of Natural Numbers
- Java Program to Find Factorial of a Number
- Java Program to Generate Multiplication Table
- Java Program to Find GCD of two Numbers
- Java Program to Find LCM of two Numbers
- Java Program to Display Alphabets (A to Z) using loop
- Java Program to Count Number of Digits in an Integer
- Java Program to Check Palindrome
- Java Program to Check Whether a Number is Prime or Not
- Java Program to Check Armstrong Number
- Java Program to Display Armstrong Number Between Two Intervals
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)