Skip to content

Commit a6959c2

Browse files
committed
feat: Implement basic calculator supporting +, -, *, /, % with exit on 'x' or 'X'
🧠 Logic: - Continuously reads operator and two operands from user - Performs the operation if valid, handles division by zero - Exits loop if user enters 'x' or 'X'. Signed-off-by: Somesh diwan <[email protected]>
1 parent e8c4066 commit a6959c2

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

Section7ConditionalStatements/Practice/src/Calculator.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
public class Calculator {
44
public static void main(String[] args) {
55
Scanner in = new Scanner(System.in);
6-
// Take input from user till user does not press X or x
6+
77
int ans = 0;
88
while (true) {
9-
// take the operator as input
109
System.out.print("Enter the operator: ");
1110
char op = in.next().trim().charAt(0);
1211

1312
if (op == '+' || op == '-' || op == '*' || op == '/' || op == '%') {
14-
// input two numbers
1513
System.out.print("Enter two numbers: ");
1614
int num1 = in.nextInt();
1715
int num2 = in.nextInt();

0 commit comments

Comments
 (0)