Advertisement
stelachilikova

Operations Between Numbers

Mar 2nd, 2022
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. package ConditionalStatementsAdvanced.exercises;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class OperationsBetweenNumbers {
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int N1 = Integer.parseInt(scanner.nextLine());
  11.         int N2 = Integer.parseInt(scanner.nextLine());
  12.  
  13.         String operation = scanner.nextLine();
  14.  
  15.         double result = 0;
  16.  
  17.         if (operation.equals("+")){
  18.             result = N1 + N2;
  19.               System.out.printf("%d + %d = %.0f", N1, N2, result);
  20.  
  21.             if ( result % 2 == 0){
  22.                   System.out.println(" - even");
  23.  
  24.             }else {
  25.                 System.out.println(" - odd");
  26.  
  27.             }
  28.         }else if (operation.equals("-")){
  29.             result = N1 - N2 ;
  30.             System.out.printf("%d - %d = %.0f", N1, N2, result);
  31.             if ( result % 2 == 0){
  32.                 System.out.println(" - even");
  33.  
  34.             }else {
  35.                 System.out.println(" - odd");
  36.             }
  37.         }else if(operation.equals("*")){
  38.             result = N1 * N2;
  39.             System.out.printf("%d * %d = %.0f", N1, N2, result);
  40.             if ( result % 2 == 0){
  41.                 System.out.println(" - even");
  42.  
  43.             }else {
  44.                 System.out.println(" - odd");
  45.             }
  46.  
  47.         } else if (operation.equals("/")) {
  48.  
  49.  
  50.             if (N1 == 0){
  51.                   System.out.printf("Cannot divide %d by zero",N2);
  52.  
  53.             }else if (N2 == 0){
  54.                 System.out.printf("Cannot divide %d by zero ",N1);
  55.  
  56.             }else {
  57.                 result = N1/(N2*1.00);
  58.                 System.out.printf("%d / %d = %.2f", N1, N2,result);
  59.             }
  60.  
  61.  
  62.         }else if(operation.equals("%")){
  63.             if (N2 == 0){
  64.                 System.out.printf("Cannot divide %d by zero ",N1);
  65.  
  66.             }else {
  67.                 result = N1 % N2;
  68.                 System.out.printf("%d %% %d = %.0f", N1, N2,result);
  69.             }
  70.  
  71.  
  72.  
  73.         }
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement