In my program I need a for-each loop which counts the number of evens in the given array and increments the variable even
for each one. When I use a standard
Though there are lot many answers. You could achieve same by using java-8 streams as well. Use reducer to find out the even's count and then from length reduce the even's count to get odd's count.
See code below:-
int[] numbers = new int[8];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (int) (Math.random() * 51 + 50);
}
int even = Arrays.stream(numbers)
.reduce(0, (a, b) -> {
a += b % 2 == 0 ? 1 : 0;
return a;
});
int odd = numbers.length - even;