Advertisement
Guest User

Operations Between Numbers

a guest
Oct 6th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.74 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.  
  8.         int n1 = Integer.parseInt(scanner.nextLine());
  9.         int n2 = Integer.parseInt(scanner.nextLine());
  10.         String operator = scanner.nextLine();
  11.  
  12.         double result = 0;
  13.  
  14.         if ("+".equals(operator)) {
  15.             result = n1 + n2;
  16.             if (result % 2 == 0) {
  17.                 System.out.printf("%d + %d = %.0f - even", n1, n2, result);
  18.             } else if (result % 2 == 1) {
  19.                 System.out.printf("%d + %d = %.0f - odd", n1, n2, result);
  20.             }
  21.         } else if ("-".equals(operator)) {
  22.             result = n1 - n2;
  23.             if (result % 2 == 0) {
  24.                 System.out.printf("%d - %d = %.0f - even", n1, n2, result);
  25.             } else if (result % 2 == 1) {
  26.                 System.out.printf("%d - %d = %.0f - odd", n1, n2, result);
  27.             }
  28.         } else if ("*".equals(operator)) {
  29.             result = n1 * n2;
  30.             if (result % 2 == 0) {
  31.                 System.out.printf("%d * %d = %.0f - even", n1, n2, result);
  32.             } else if (result % 2 == 1) {
  33.                 System.out.printf("%d * %d = %.0f - odd", n1, n2, result);
  34.             }
  35.         } else if (n2 == 0 || n1 == 0) {
  36.             System.out.printf("Cannot divide %d by zero", Math.max(n1, n2));
  37.  
  38.         } else if ("/".equals(operator)) {
  39.             result = (double) n1 / (double) n2;
  40.             System.out.printf("%d / %d = %.2f", n1, n2, result);
  41.         } else if ("%".equals(operator)) {
  42.             result = n1 % n2;
  43.             System.out.println(n1 + " % " + n2 + " = " + (int) result);
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement