EliPerfanova

Operations Between Numbers

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