Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lekcii;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class EvenAndOddSubtraction {
- public static int calculateDifference(int[] numbers) {
- int sumEven = 0;
- int sumOdd = 0;
- for (int number : numbers) {
- if (number % 2 == 0) {
- sumEven += number;
- } else {
- sumOdd += number;
- }
- }
- return sumEven - sumOdd;
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read input as a space-separated string
- String input = scanner.nextLine();
- // Split the input string into an array of strings
- String[] strNumbers = input.split(" ");
- // Convert the array of strings to an array of integers
- int[] array = new int[strNumbers.length];
- for (int i = 0; i < strNumbers.length; i++) {
- array[i] = Integer.parseInt(strNumbers[i]);
- }
- int difference = calculateDifference(array);
- System.out.println(difference);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement