**Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array.** Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.Arrays; public class Example3 { static int f (int [] a) { int oddSum=0; int evenSum=0; for(int i=0;i<a.length;i++) { if(a[i]%2==0) { evenSum=evenSum+a[i]; } else { oddSum=oddSum+a[i]; } } return oddSum-evenSum; } public static void main(String [] args) { int [] array={1}; System.out.println(f(array)); int [] array2={3, 2, 3, 4}; System.out.println(f(array2)); } } |