forked from MAYANK25402/Hacktober-Fest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
33 lines (31 loc) · 874 Bytes
/
Copy pathCalculator.java
File metadata and controls
33 lines (31 loc) · 874 Bytes
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
import java.util.*;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
int numA = input.nextInt();
System.out.print("Enter second number: ");
int numB = input.nextInt();
System.out.print("Enter operator [+, -, *, /]: ");
char oper = input.next().charAt(0);
long result = calculate(numA, numB, oper);
System.out.println("Result: " + result);
}
private static long calculate(int A, int B, char oper) {
switch (oper) {
case '+':
return A + B;
case '-':
return A - B;
case '*':
case 'x':
return (A * 1l) * (B * 1l);
case '/':
if (B == 0) return 0;
return A / B;
default:
System.out.println("Wrong Input");
return -1;
}
}
}