Answers for "java calculator program using switch case"

0

how to make a calculator using switch case in java

/* Create Switch cases */
Scanner sc= new Scanner(System.in);
int firstNum= sc.nextInt();
int secondNum= sc.nextInt();
System.out.println("Enter an operator (+, -, *, /):");
char operator= sc.next().chartAt(0);
double result=0;
switch(operator){
  case "+":
    result+= firstNum+ secondNum;
    break;
  case "-":
    result+= firstNum-secondNum;
    break;
  case "*":
    result+= firstNum*secondNum;
    break;
  case "/":
    result+= firstNum/secondNum;
    break;
}
Posted by: Guest on November-15-2020
0

Java program for a calculator using switch case

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);
}
}
Posted by: Guest on July-03-2021

Code answers related to "java calculator program using switch case"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language