Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update calculator.java #6993

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 55 additions & 29 deletions calculator.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,60 @@
import java.util.*;

public class calculator{
public static void main(String args[]){
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter a : ");
int a = sc.nextInt();
System.out.println("Enter b : ");
int b = sc.nextInt();
System.out.println("Enter operator : ");

char operator = sc.next().charAt(0);


switch(operator){

case '+' : System.out.println(a+b);
break;
case '-' : System.out.println(a-b);
break;
case '/' : System.out.println(a/b);
break;
case '*' : System.out.println(a*b);
break;
case '%' : System.out.println(a%b);
break;
default : System.out.println("wrong operator");

boolean continueCalculating = true;

while (continueCalculating) {
System.out.println("Enter a: ");
int a = sc.nextInt();
System.out.println("Enter b: ");
int b = sc.nextInt();
System.out.println("Enter operator: ");
char operator = sc.next().charAt(0);

int result = 0;
switch (operator) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '/':
if (b != 0) {
result = a / b;
} else {
System.out.println("Division by zero is not allowed.");
continue;
}
break;
case '*':
result = a * b;
break;
case '%':
if (b != 0) {
result = a % b;
} else {
System.out.println("Modulo by zero is not allowed.");
continue;
}
break;
default:
System.out.println("Wrong operator");
continue;
}

System.out.println("Result: " + result);

// Ask if the user wants to continue
System.out.println("Do you want to perform another calculation? (Y/N)");
char choice = sc.next().charAt(0);
if (choice != 'Y' && choice != 'y') {
continueCalculating = false;
}
}
sc.close();

}
}
sc.close();
}
}
Loading