-2

I want to find the number of even numbers in an array of integers. so far i wrote this code

public static int EvenNum(int[] arr) {
    int index = 0;
    if (arr[index] % 2 == 1) {
        return 0;
    } else if (arr[index] % 2 == 0) {
        return 1;
    }
    index++;
    return index + EvenNum(arr);
}

please help me make this code run

Songul
  • 45
  • 1
  • 3
  • 9
  • What does help me run imply? Get you a compiler? – devnull Mar 27 '14 at 17:56
  • What _specifically_ doesn't you understand? Have you used a debugger? – Sotirios Delimanolis Mar 27 '14 at 17:57
  • 2
    `please help me make this code run` try this :) `public static void main(String[] args){System.out.println(EvenNum(new int[]{1,2,3,4,5}));}` – Pshemo Mar 27 '14 at 17:57
  • why do you need recursion at all? – laymelek Mar 27 '14 at 18:04
  • each time i run the code i get " " (nothing but space) @Pshemo – Songul Mar 27 '14 at 18:42
  • when i run the meathod i get space, i dont know why i can't get any number or the counting.. here is what i wrote in my main method System.out.println("The Even Numbers in array are: "); EvenNum(array); /// i asked my teacher and he told me that i am counting even number. my logic is correct but this time, the index is used to control the recursion call..... i dont understand what he meant by that @Pshemo – Songul Mar 27 '14 at 18:57
  • i need recursion because its a HW @laymelek – Songul Mar 27 '14 at 18:59
  • no i dont use debugger @Sotirios Delimanolis , actually i dont know what is debugger, Im new n coding – Songul Mar 27 '14 at 19:00
  • @user3469667 in your code result of `EvenNum(array);` is outside of `System.out.println(...)` which means it will not get printed. You need to place it inside parenthesis and concatenate it with your String information (use `+` operator) like `System.out.println("The Even Numbers in array are: "+EvenNum(array));` – Pshemo Mar 27 '14 at 19:18

2 Answers2

2

You can use an index parameter to know where in the array you are checking.

public static int EvenNum(int[] arr, int index)
{
    if (index == arr.length) return 0;  // Stop recursion
    //return (arr[index] % 2 == 0 ? 1 : 0) + EvenNum(arr, index + 1);
    // or....
    int result;
    if ((arr[index] % 2) == 0) {    // Is even
        result = 1;
    }
    else {                          // Is odd
        result = 0;
    }
    return result + EvenNum(arr, index + 1);
}

public static void main(final String... args)
{
    // Start with index = 0
    System.out.println(EvenNum(new int[]{1,2,3,4,5}, 0));
} 
001
  • 13,291
  • 5
  • 35
  • 66
  • please simplfy this (arr[index] % 2 == 0 ? 1 : 0) because im new at coding and i dont know what u mean by ? 1 : 0 @Johnny Mopp – Songul Mar 27 '14 at 19:01
  • @user3469667 Updated my answer. Read [this](http://stackoverflow.com/questions/392932/how-do-i-use-the-conditional-operator) for info on the ternary operator. – 001 Mar 27 '14 at 19:09
  • can you explain or simplify arr[index] % 2 == 0 ? 1 : 0.......... because im new at coding and i dont understand what ? 1 : 0 means @Johnny Mopp – Songul Mar 27 '14 at 19:09
-1

make use of a loop inside the your EvenNum() method:

int evenNoCount = 0;
for (int i=0; i<arr.length();i++){
    if(arr[i]%2 == 0)
        evenNoCount++;
}
return evenNoCount;
Piko
  • 31
  • 1