Skip to content

Instantly share code, notes, and snippets.

@john-nash-rs
Created July 24, 2023 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-nash-rs/99abb746d96d01a08296902ae42c2396 to your computer and use it in GitHub Desktop.
Save john-nash-rs/99abb746d96d01a08296902ae42c2396 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class ArithmeticCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter the operation (+, -, *, /): ");
char operation = scanner.next().charAt(0);
double result = 0.0;
switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division by zero is not allowed.");
return;
}
break;
default:
System.out.println("Error: Invalid operation.");
return;
}
System.out.println("The result is: " + result);
scanner.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment