Share your repls and programming experiences

← Back to all posts
Java program for a calculator using switch case
RaghunathThilag (1)

Java program for a calculator using switch case. In Java programming, build a simple calculator program. This is a straightforward calculator. Basic operations such as add, sub, mul, and div will be performed. The java basic calculator software is intended only for beginners who want to learn how to use the java programming language to perform calculations. It is self-evident that a calculator can relieve the consumer of the need to perform mental calculations and rely on paper

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
System.out.print("Enter two numbers");
double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.print("Enter an operators");
char operator = reader.next().charAt(0);
double result;
switch(operator)
{
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '':
result = first
second;
break;
case '/':
result = first / second;
break;
default:
System.out.printf("operator is correct");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
}
}

Output

Enter two numbers

4 5 9

Link: https://inlarn.com/java-program-for-a-calculator-using-switch-case/