Advertisement
svephoto

Operations Between Numbers [Java]

Apr 16th, 2021 (edited)
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 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 symbol = scanner.nextLine();
  10.  
  11.         double result;
  12.  
  13.         if (symbol.equals("+")) {
  14.             result = n1 + n2;
  15.  
  16.             if (result % 2 == 0) {
  17.                 System.out.printf("%d %s %d = %.0f - even", n1, symbol, n2, result);
  18.             } else {
  19.                 System.out.printf("%d %s %d = %.0f - odd", n1, symbol, n2, result);
  20.             }
  21.         } else if (symbol.equals("-")) {
  22.             result = n1 - n2;
  23.  
  24.             if (result % 2 == 0) {
  25.                 System.out.printf("%d %s %d = %.0f - even", n1, symbol, n2, result);
  26.             } else {
  27.                 System.out.printf("%d %s %d = %.0f - odd", n1, symbol, n2, result);
  28.             }
  29.         } else if (symbol.equals("*")) {
  30.             result = n1 * n2;
  31.  
  32.             if (result % 2 == 0) {
  33.                 System.out.printf("%d %s %d = %.0f - even", n1, symbol, n2, result);
  34.             } else {
  35.                 System.out.printf("%d %s %d = %.0f - odd", n1, symbol, n2, result);
  36.             }
  37.         } else if (symbol.equals("/")) {
  38.             if (n2 == 0) {
  39.                 System.out.printf("Cannot divide %d by zero", n1);
  40.             } else {
  41.                 result = n1 * 1.0 / n2;
  42.                 System.out.printf("%d / %d = %.2f", n1, n2, result);
  43.             }
  44.         } else if (symbol.equals("%")) {
  45.             if (n2 == 0) {
  46.                 System.out.printf("Cannot divide %d by zero", n1);
  47.             } else {
  48.                 result = n1 % n2;
  49.                 System.out.printf("%d %% %d = %.0f", n1, n2, result);
  50.             }
  51.         }
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement