File tree 1 file changed +75
-0
lines changed
1 file changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Java program for simple calculator
2
+
3
+ import java .io .*;
4
+ import java .lang .*;
5
+ import java .lang .Math ;
6
+ import java .util .Scanner ;
7
+ public class BasicCalculator {
8
+
9
+ public static void main (String [] args )
10
+ {
11
+ // stores two numbers
12
+ double num1 , num2 ;
13
+
14
+ // Take input from the user
15
+ Scanner sc = new Scanner (System .in );
16
+
17
+ System .out .println ("Enter the numbers" );
18
+
19
+ // take the inputs
20
+ num1 = sc .nextDouble ();
21
+
22
+ num2 = sc .nextDouble ();
23
+
24
+ System .out .println ("Enter the operator (+,-,*,/)" );
25
+
26
+ char op = sc .next ().charAt (0 );
27
+
28
+ double o = 0 ;
29
+
30
+ switch (op ) {
31
+
32
+ // case to add two numbers
33
+ case '+' :
34
+
35
+ o = num1 + num2 ;
36
+
37
+ break ;
38
+
39
+ // case to subtract two numbers
40
+ case '-' :
41
+
42
+ o = num1 - num2 ;
43
+
44
+ break ;
45
+
46
+ // case to multiply two numbers
47
+ case '*' :
48
+
49
+ o = num1 * num2 ;
50
+
51
+ break ;
52
+
53
+ // case to divide two numbers
54
+ case '/' :
55
+
56
+ o = num1 / num2 ;
57
+
58
+ break ;
59
+
60
+ default :
61
+
62
+ System .out .println ("You enter wrong input" );
63
+
64
+ break ;
65
+ }
66
+
67
+ System .out .println ("The final result:" );
68
+
69
+ System .out .println ();
70
+
71
+ // print the final result
72
+ System .out .println (num1 + " " + op + " " + num2
73
+ + " = " + o );
74
+ }
75
+ }
You can’t perform that action at this time.
0 commit comments