-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperators.java
More file actions
28 lines (25 loc) · 952 Bytes
/
Operators.java
File metadata and controls
28 lines (25 loc) · 952 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
// Take in two numbers and an operator (+, -, *, /) and calculate the value. (Use if conditions)
import java.util.Scanner;
public class Operators {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number : ");
double num1 = sc.nextDouble();
System.out.print("Enter the second number : ");
double num2 = sc.nextDouble();
System.out.print("Enter the operator (+, -, *, /) : ");
String operator = sc.next();
double result = 0;
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
result = num1 / num2;
}
System.out.println("The result is " + result);
sc.close();
}
}