File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Section7ConditionalStatements/Practice/src Expand file tree Collapse file tree 1 file changed +44
-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 in = new Scanner (System .in );
6+ // Take input from user till user does not press X or x
7+ int ans = 0 ;
8+ while (true ) {
9+ // take the operator as input
10+ System .out .print ("Enter the operator: " );
11+ char op = in .next ().trim ().charAt (0 );
12+
13+ if (op == '+' || op == '-' || op == '*' || op == '/' || op == '%' ) {
14+ // input two numbers
15+ System .out .print ("Enter two numbers: " );
16+ int num1 = in .nextInt ();
17+ int num2 = in .nextInt ();
18+
19+ if (op == '+' ) {
20+ ans = num1 + num2 ;
21+ }
22+ if (op == '-' ) {
23+ ans = num1 - num2 ;
24+ }
25+ if (op == '*' ) {
26+ ans = num1 * num2 ;
27+ }
28+ if (op == '/' ) {
29+ if (num2 != 0 ) {
30+ ans = num1 / num2 ;
31+ }
32+ }
33+ if (op == '%' ) {
34+ ans = num1 % num2 ;
35+ }
36+ } else if (op == 'x' || op == 'X' ) {
37+ break ;
38+ } else {
39+ System .out .println ("Invalid operation!!" );
40+ }
41+ System .out .println (ans );
42+ }
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments