-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculatorUsingIfElse.java
More file actions
33 lines (29 loc) · 1.09 KB
/
calculatorUsingIfElse.java
File metadata and controls
33 lines (29 loc) · 1.09 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
import java.util.Scanner;
public class calculatorUsingIfElse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input
System.out.print("Enter the first number: ");
int a = sc.nextInt();
System.out.print("Enter the operation (+, -, *, /): ");
char op = sc.next().charAt(0);
System.out.print("Enter the second number: ");
int b = sc.nextInt();
// Calculation and Output
if (op == '+') {
System.out.println("Result: " + (a + b));
} else if (op == '-') {
System.out.println("Result: " + (a - b));
} else if (op == '*') {
System.out.println("Result: " + (a * b));
} else if (op == '/') {
if (b != 0) {
System.out.println("Result: " + (a / b));
} else {
System.out.println("Error: Division by zero is not allowed.");
}
} else {
System.out.println("Error: Invalid operation. Please enter +, -, *, or /.");
}
}
}