Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class Operations2 {
- 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();
- BigDecimal result = new BigDecimal("0.00");
- String output = "";
- DecimalFormat intFormater = new DecimalFormat("0");
- if (num2 == 0 && (operator.equals("/") || operator.equals("%"))) {
- output = String.format("Cannot divide %d by zero", num1);
- } else if (operator.equals("/")) {
- result = new BigDecimal(num1).divide(new BigDecimal(num2), 2, RoundingMode.HALF_UP);
- DecimalFormat decimalFormater = new DecimalFormat("0.00");
- output = String.format("%d %s %d = %s", num1, operator, num2, decimalFormater.format(result));
- } else if (operator.equals("%")) {
- result = new BigDecimal(num1).remainder(new BigDecimal(num2));
- output = String.format("%d %s %d = %s", num1, operator, num2, intFormater.format(result));
- } else {
- if (operator.equals("+")) {
- result = new BigDecimal(num1 + num2);
- } else if (operator.equals("-")) {
- result = new BigDecimal(num1 - num2);
- } else if (operator.equals("*")) {
- result = new BigDecimal(num1 * num2);
- }
- output = String.format("%d %s %d = %s - %s", num1, operator, num2, intFormater.format(result),
- result.intValueExact() % 2 == 0 ? "even" : "odd");
- }
- System.out.println(output);
- }
- }
Add Comment
Please, Sign In to add comment