Advertisement
svephoto

Operations Between Numbers [Java]

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