0% found this document useful (0 votes)
27 views2 pages

Arithmetic Calculator

This Java program defines a calculator class that uses a switch statement to perform basic arithmetic operations (+, -, *, /) on two user-inputted numbers. The user is prompted to enter an operator and two numbers, and the program outputs the calculation result in sentence form by printing the two numbers, operator, and result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Arithmetic Calculator

This Java program defines a calculator class that uses a switch statement to perform basic arithmetic operations (+, -, *, /) on two user-inputted numbers. The user is prompted to enter an operator and two numbers, and the program outputs the calculation result in sentence form by printing the two numbers, operator, and result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ARITHMETIC CALCULATOR

package jan.java;
import java.util.Scanner;

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 an operator: +, -, *, or /");
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 operator!");
break;
}

input.close();
}
}

You might also like