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