John Alex John Alex - 2 months ago 9
Java Question

How to determine whether a number is even or odd and then count them in Java

Extremely new to Java, have just started using it in the last 2 weeks in school, we've been given the assignment to make a program to determine whether 5 given intergers are even or odd and then give an output to look like this
Even: 2
Odd: 3
We need to use the modulo operator and switch statements, and i'm just not sure how to go about this, any help would be appreciated.

Answer

Let us say you got 5 numbers. Create the array for them and then loop through it. Check every element if the remainder of division by 2 is 0 or 1.

int even = 0;
int odd = 0;

int[] array = new int[5];

for (int i=0; i<array.length; i++)
    if (array[i]%2 == 0)
        even++;
    else
        odd++;