Advertisement
GabrielHr00

06. Operations Between Numbers

May 28th, 2023
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package S3_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.             if (result % 2 == 0){
  15.                 System.out.printf("%d %c %d = %d - even", n1, operator, n2, result);
  16.             } else {
  17.                 System.out.printf("%d %c %d = %d - odd", n1, operator, n2, result);
  18.             }
  19.         } else if (operator == '-') {
  20.             int result = n1 - n2;
  21.             if (result % 2 == 0){
  22.                 System.out.printf("%d %c %d = %d - even", n1, operator, n2, result);
  23.             } else {
  24.                 System.out.printf("%d %c %d = %d - odd", n1, operator, n2, result);
  25.             }
  26.         } else if (operator == '*') {
  27.             int result = n1 * n2;
  28.             if (result % 2 == 0){
  29.                 System.out.printf("%d %c %d = %d - even", n1, operator, n2, result);
  30.             } else {
  31.                 System.out.printf("%d %c %d = %d - odd", n1, operator, n2, result);
  32.             }
  33.         }
  34.         else if (operator == '/') {
  35.             if(n2 <= 0) {
  36.                 System.out.printf("Cannot divide %d by zero", n1);
  37.             } else {
  38.                 double result = (1.0 * n1) / n2;
  39.                 System.out.printf("%d / %d = %.2f", n1, n2, result);
  40.             }
  41.         }
  42.         else if (operator == '%') {
  43.             if(n2 <= 0) {
  44.                 System.out.printf("Cannot divide %d by zero", n1);
  45.             } else {
  46.                 int result = n1 % n2;
  47.                 System.out.printf("%d %% %d = %d", n1, n2, result);
  48.             }
  49.         }
  50.     }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement