Advertisement
Spocoman

05. Even and Odd Subtraction

Oct 20th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int[] numbers = Arrays
  9.                 .stream(scanner.nextLine().split(" "))
  10.                 .mapToInt(Integer::parseInt)
  11.                 .toArray();
  12.  
  13.         int evenSum = 0, oddSum = 0;
  14.  
  15.         for (int n : numbers) {
  16.             if (n % 2 == 0) {
  17.                 evenSum += n;
  18.             } else {
  19.                 oddSum += n;
  20.             }
  21.         }
  22.  
  23.         System.out.println(evenSum - oddSum);
  24.     }
  25. }
  26.  
  27. OR:
  28.  
  29. import java.util.Scanner;
  30. import java.util.Arrays;
  31.  
  32. class Main {
  33.     public static void main(String[] args) {
  34.         Scanner scanner = new Scanner(System.in);
  35.  
  36.         int[] numbers = Arrays
  37.                 .stream(scanner.nextLine().split(" "))
  38.                 .mapToInt(Integer::parseInt)
  39.                 .toArray();
  40.  
  41.         int evenSum = Arrays.stream(numbers).filter(e -> e % 2 == 0).sum();
  42.         int oddSum = Arrays.stream(numbers).filter(e -> e % 2 == 1).sum();
  43.        
  44.         System.out.println(evenSum - oddSum);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement