Advertisement
desislava_topuzakova

06. Operations Between Numbers

Jun 23rd, 2020
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class OperationsBetweenNumbers_06 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int num1 = Integer.parseInt(scanner.nextLine());
  7.         int num2 = Integer.parseInt(scanner.nextLine());
  8.         String operator = scanner.nextLine();
  9.  
  10.         //+, -, *, /, %
  11.         switch (operator) {
  12.             case "+":
  13.                 //събиране
  14.                 int sum = num1 + num2;
  15.                 //четно или нечетно
  16.                 if (sum % 2 == 0) {
  17.                     System.out.printf("%d + %d = %d - even", num1, num2, sum);
  18.                 } else {
  19.                     System.out.printf("%d + %d = %d - odd", num1, num2, sum);
  20.                 }
  21.                 break;
  22.             case "-":
  23.                 //изваждане
  24.                 int diff = num1 - num2;
  25.                 if (diff % 2 == 0) {
  26.                     System.out.printf("%d - %d = %d - even", num1, num2, diff);
  27.                 } else {
  28.                     System.out.printf("%d - %d = %d - odd", num1, num2, diff);
  29.                 }
  30.                 break;
  31.             case "*":
  32.                 //умножение
  33.                 int result = num1 * num2;
  34.                 if (result % 2 == 0) {
  35.                     System.out.printf("%d * %d = %d - even", num1, num2, result);
  36.                 } else {
  37.                     System.out.printf("%d * %d = %d - odd", num1, num2, result);
  38.                 }
  39.                 break;
  40.             case "/":
  41.                 if (num2 == 0) {
  42.                     System.out.printf("Cannot divide %d by zero", num1);
  43.                 } else { //num2 != 0
  44.                     //деление -> делителя да е различен от 0 и типовете на числата
  45.                     double division = num1 * 1.0 / num2;
  46.                     System.out.printf("%d / %d = %.2f", num1, num2, division);
  47.                 }
  48.                 break;
  49.             case "%":
  50.                 if (num2 == 0) {
  51.                     System.out.printf("Cannot divide %d by zero", num1);
  52.                 } else {
  53.                     //модулно деление
  54.                     int leftover = num1 % num2;
  55.                     System.out.printf("%d %% %d = %d", num1, num2, leftover);
  56.                 }
  57.                 break;
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement