Advertisement
Ligh7_of_H3av3n

EvenAndOddSubtraction

Jan 24th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. package Lekcii;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class EvenAndOddSubtraction {
  7.  
  8.     public static int calculateDifference(int[] numbers) {
  9.         int sumEven = 0;
  10.         int sumOdd = 0;
  11.  
  12.         for (int number : numbers) {
  13.             if (number % 2 == 0) {
  14.                 sumEven += number;
  15.             } else {
  16.                 sumOdd += number;
  17.             }
  18.         }
  19.  
  20.         return sumEven - sumOdd;
  21.     }
  22.  
  23.     public static void main(String[] args) {
  24.         Scanner scanner = new Scanner(System.in);
  25.  
  26.  
  27.         // Read input as a space-separated string
  28.         String input = scanner.nextLine();
  29.  
  30.         // Split the input string into an array of strings
  31.         String[] strNumbers = input.split(" ");
  32.  
  33.         // Convert the array of strings to an array of integers
  34.         int[] array = new int[strNumbers.length];
  35.         for (int i = 0; i < strNumbers.length; i++) {
  36.             array[i] = Integer.parseInt(strNumbers[i]);
  37.         }
  38.  
  39.         int difference = calculateDifference(array);
  40.  
  41.         System.out.println(difference);
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement