-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathMyCal.java
More file actions
50 lines (37 loc) · 1.07 KB
/
MyCal.java
File metadata and controls
50 lines (37 loc) · 1.07 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package codingExamples;
import java.util.Scanner;
// This Java program performs operations of a basic calculator
public class MyCal {
public static void main(String[] args) {
// take user input
Scanner reader = new Scanner(System.in);
System.out.print("Enter any two numbers: ");
double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = reader.next().charAt(0);
double result;
//To perform the calculation operation for each operator
switch(operator)
{
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
// if operator doesn't match any case
default:
System.out.printf("Error! invalied operator. Please try again.");
return;
}
//printing the result of the operations
System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
}
}