SvetlanPetrova

Operations Between Numbers SoftUni

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