-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_calculator.java
More file actions
33 lines (30 loc) · 1.03 KB
/
Copy pathsimple_calculator.java
File metadata and controls
33 lines (30 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package Developer;
import java.util.Scanner;
public class simple_calculator {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
double a = sc.nextDouble();
System.out.println("Enter Operator (+,-, *, /): ");
char op = sc.next().charAt(0);
System.out.println("Enter Second Number: ");
double b = sc.nextInt();
double result;
switch (op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/':
if(b == 0) {
System.out.println("Error! Division by zero.");
return;
}
result = a / b; break;
default:
System.out.println("Invalid Operator!");
return;
}
System.out.println("Result: " + a + " " + op + " " + b + " = " + result);
sc.close();
}
}