0% found this document useful (0 votes)
47 views

Calculator

This Java program defines a Calculator class with a main method that allows a user to choose an arithmetic operator and input two numbers to perform a calculation on. The program uses a switch statement to evaluate the operator and perform the addition, subtraction, multiplication, or division of the two numbers as selected by the user, printing the equation and result.

Uploaded by

Andreea Liliana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Calculator

This Java program defines a Calculator class with a main method that allows a user to choose an arithmetic operator and input two numbers to perform a calculation on. The program uses a switch statement to evaluate the operator and perform the addition, subtraction, multiplication, or division of the two numbers as selected by the user, printing the equation and result.

Uploaded by

Andreea Liliana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

public class Calculator {

public static void main(String[] args) {


char operator;
Double number1, number2, result;

Scanner input = new Scanner(System.in);

System.out.println("Choose the calculation operation you need: +, -,


*, /");
operator = input.next().charAt(0);

System.out.println("Enter first number");


number1 = input.nextDouble();
System.out.println("Enter second number");
number2 = input.nextDouble();

switch (operator) {
case '+':
result = number1 + number2;
System.out.println(number1 + "+" + number2 + "=" + result);
break;
case '-':
result = number1 - number2;
System.out.println(number1 + "-" + number2 + "=" + result);
break;
case '*':
result = number1 * number2;
System.out.println(number1 + "*" + number2 + "=" + result);
break;
case '/':
result = number1 / number2;
System.out.println(number1 + "/" + number2 + "=" + result);
break;
default:
System.out.println("Invalid operation!");
break;
}
input.close();
}
}

You might also like