borkins

Exam 24-Apr-2016 - 03. Operations between Numbers

May 16th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. /**
  2.  * Project: Exam_24_April_2016 - created by borkins on 2017-05-16.
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class _03_OperationsBetweenNumbers
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         Scanner console = new Scanner(System.in);
  12.        
  13.         int a = Integer.parseInt(console.nextLine());
  14.         int b = Integer.parseInt(console.nextLine());
  15.         String op = console.nextLine();
  16.        
  17.         if (b == 0 && (op.equals("/") || op.equals("%"))) {
  18.             System.out.println("Cannot divide " + a + " by zero");
  19.         } else if (op.equals("+")) {
  20.             System.out.printf("%d + %d = %d - %s%n", a, b, (a + b), oddEven(a + b));
  21.         } else if (op.equals("-")) {
  22.             System.out.printf("%d - %d = %d - %s%n", a, b, (a - b), oddEven(a - b));
  23.         } else if (op.equals("*")) {
  24.             System.out.printf("%d * %d = %d - %s%n", a, b, (a * b), oddEven(a * b));
  25.         } else if (op.equals("/")) {
  26.             System.out.printf("%d / %d = %.2f%n", a, b, ((double) a / b));
  27.         } else if (op.equals("%")) {
  28.             System.out.printf("%d %% %d = %d%n", a, b, (a % b));
  29.         }
  30.     }
  31.    
  32.     public static String oddEven(int result) {
  33.         return result % 2 == 0 ? "even" : "odd";
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment