60 678
Assignments Done
97,7%
Successfully Done
In April 2018

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
Expert's answer
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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions