Advertisement
CoineTre

JF-LabArrays05.EvenOddSubtraction

Jan 18th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Lab5EvenOddSubtraction {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String input = scanner.nextLine();
  7.         String[] inputNumber = input.split(" ");
  8.         int[] array = new int[inputNumber.length];
  9.         int evenSum = 0;
  10.         int oddSum = 0;
  11.         for (int i = 0; i < array.length ; i++) {
  12.             array[i]=Integer.parseInt(inputNumber[i]);
  13.         }
  14.         for (int i : array) {// for each loop
  15.             if (i % 2==0){
  16.                 evenSum+=i;
  17.             }else {
  18.                 oddSum += i;
  19.             }
  20.         }
  21.         int subtraction = evenSum - oddSum;
  22.         System.out.println(subtraction);
  23.     }
  24. }
  25.  
  26.   /*  Write a program that calculates the difference between the sum of the even
  27.   and the sum of the odd numbers in an array.*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement