Answer to Question #2154 in Java | JSP | JSF for Array method

Question #2154
Create a method called countOdds which takes an integer array as a parameter, and returns the number of odd entries in the array.


Examples:
theArray:{10, 3, 6, 3, 8, 10} ==> 2
theArray:{11, 3, 6, 7, 9, 60} ==> 4
1
Expert's answer
2011-03-29T12:35:57-0400
public class Main { public static void main(String[] args) {
int[] array1 = {10, 3, 6, 3, 8, 10};
int[] array2 = {11, 3, 6, 7, 9, 60};
System.out.println(countOdds(array1));
System.out.println(countOdds(array2));
}

public static int countOdds(int[] array) {
int oddNum = 0;
for(int i = 0; i < array.length; i++) {
if(array[i] % 2 == 1) {
oddNum++;
}
}
return oddNum;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS