File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class Calculator {
4+ public static void main (String [] args ) {
5+ Scanner sc = new Scanner (System .in );
6+
7+ System .out .println ("=== Simple Calculator ===" );
8+ System .out .print ("Enter first number: " );
9+ double num1 = sc .nextDouble ();
10+
11+ System .out .print ("Enter second number: " );
12+ double num2 = sc .nextDouble ();
13+
14+ System .out .println ("Choose an operation: + - * /" );
15+ char operator = sc .next ().charAt (0 );
16+
17+ double result ;
18+
19+ switch (operator ) {
20+ case '+' :
21+ result = num1 + num2 ;
22+ break ;
23+
24+ case '-' :
25+ result = num1 - num2 ;
26+ break ;
27+
28+ case '*' :
29+ result = num1 * num2 ;
30+ break ;
31+
32+ case '/' :
33+ if (num2 != 0 )
34+ result = num1 / num2 ;
35+ else {
36+ System .out .println ("Error! Division by zero is not allowed." );
37+ sc .close ();
38+ return ;
39+ }
40+ break ;
41+
42+ default :
43+ System .out .println ("Invalid operator!" );
44+ sc .close ();
45+ return ;
46+ }
47+
48+ System .out .println ("Result: " + result );
49+ sc .close ();
50+ }
51+ }
You can’t perform that action at this time.
0 commit comments