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.
switch
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."
6.5 * 2
6.5 * 2 = 13.00
import java.util.Scanner; public class BasicCalculatorSwitch { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the first number: "); double a = input.nextDouble(); // Allowing decimal numbers. System.out.print("Enter the operation (+, -, x, /): "); char operation = input.next().charAt(0); System.out.print("Enter the second number: "); double b = input.nextDouble(); // Allowing decimal numbers. double result = 0.0; switch (operation) { case '+': result = a + b; System.out.printf("%.2f + %.2f = %.2f%n", a, b, result); break; case '-': result = a - b; System.out.printf("%.2f - %.2f = %.2f%n", a, b, result); break; case 'x': case '*': result = a * b; System.out.printf("%.2f x %.2f = %.2f%n", a, b, result); break; case '/': if (b != 0) { result = a / b; System.out.printf("%.2f / %.2f = %.2f%n", a, b, result); } else { System.out.println("Division by zero not allowed"); } break; default: System.out.println("Character not recognized"); } } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Java basic calculator. Input numbers and operators (+, -, x, /) for calculation. If the symbol is not valid, it displays 'Character not recognized'.
Calculate and display the absolute value of a number in Java with this programming exercise on conditions.
Basic calculator with switch for mathematical operations. Allows decimal numbers and error handling. Practice your skills in Java.
Learn Java: Detect whether a number is positive or negative in this programming exercise. Enhance your Java skills.
Create a Java program that prompts the user for a letter (x) and checks whether it is a vowel or any other symbol.
Create a Java program that uses the conditional operator to assign value to a boolean variable.
Java exercise to find the largest number among three and practice Java conditions.
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.
Calculate a student's grade using a switch statement in Java based on an integer input. Practice using switch, break, and default.
Practice with exercises in Java related to the conditions, loops and structures of the language.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.