Advertisement
GabrielHr00

06. Operations Between Numbers

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