Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.util.Scanner;
- public class Operations {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num1 = Integer.parseInt(scanner.nextLine());
- int num2 = Integer.parseInt(scanner.nextLine());
- String operator = scanner.nextLine();
- String output = "";
- if (num2 == 0 && (operator.equals("/") || operator.equals("%"))) {
- output = String.format("Cannot divide %d by zero", num1);
- } else {
- switch (operator) {
- case "+":
- output = String.format("%d %s %d = %d - %s", num1, operator, num2, (num1 + num2),
- new BigDecimal(num1 + num2).intValueExact() % 2 == 0 ? "even" : "odd");
- break;
- case "-":
- output = String.format("%d %s %d = %d - %s", num1, operator, num2, (num1 - num2),
- new BigDecimal(num1 - num2).intValueExact() % 2 == 0 ? "even" : "odd");
- break;
- case "*":
- output = String.format("%d %s %d = %d - %s", num1, operator, num2, (num1 * num2),
- new BigDecimal(num1 * num2).intValueExact() % 2 == 0 ? "even" : "odd");
- break;
- case "/":
- output = String.format("%d %s %d = %.2f", num1, operator, num2, ((num1 * 1.0) / num2));
- break;
- case "%":
- output = String.format("%d %s %d = %d", num1, operator, num2, (num1 % num2));
- break;
- default:
- System.out.println("Unknown operator");
- }
- }
- System.out.println(output);
- }
- }
Add Comment
Please, Sign In to add comment