Basic calculator using switch


Tuesday, October 31, 2023 Difficulty: Easy

Objective

Write a Java program that functions as a basic calculator. This program allows the user to perform simple mathematical operations with two numbers. The user will input two numbers and then select an operation (addition (+), subtraction (-), multiplication (x or *), or division (/)) using a switch block. The program will perform the selected operation and display the result on the screen.

We add complexity by allowing decimal operations instead of integers. If the user inputs a decimal number or if the operation results in a decimal number, the program should display the result with two decimal places.

Additionally, if the user attempts to divide by zero, the program should indicate "Division by zero not allowed."

If the operation symbol is different from the allowed operators (+, -, x, /), the program will indicate "Character not recognized."

Input

6.5
*
2

Output

6.5 * 2 = 13.00

Solution

Click here to view the exercise solution

Share it

Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.

Copied

The code has been successfully copied to the clipboard.

More Exercises

Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!

1

Basic calculator using if

Java basic calculator. Input numbers and operators (+, -, x, /) for calculation. If the symbol is not valid, it displays 'Character not recognized'.

2

ABS - Absolute value

Calculate and display the absolute value of a number in Java with this programming exercise on conditions.

3

Basic calculator using switch

Basic calculator with switch for mathematical operations. Allows decimal numbers and error handling. Practice your skills in Java.

4

Positive and negative

Learn Java: Detect whether a number is positive or negative in this programming exercise. Enhance your Java skills.

5

Check vowels with switch

Create a Java program that prompts the user for a letter (x) and checks whether it is a vowel or any other symbol.

6

boolean and conditional operator

Create a Java program that uses the conditional operator to assign value to a boolean variable.

7

The largest of three numbers

Java exercise to find the largest number among three and practice Java conditions.

8

Conditional operator

9

Nested conditional operator

Create a Java program that prompts the user to enter two integers (a, b) and determines how many of them are positive using nested conditional operators in Java.

10

Switch, break, and default

Calculate a student's grade using a switch statement in Java based on an integer input. Practice using switch, break, and default.

Flow controls: Conditions

Practice with exercises in Java related to the conditions, loops and structures of the language.

Practice exercises

Keep learning

C# Programming Course

Free programming course with practical exercises and solutions in C#. Start learning right now!

Exercises C# App

Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play