Write method countEvens which returns the number of even integers found in its a
ID: 3545022 • Letter: W
Question
Write method countEvens which returns the number of even integers found in its array parameter. Fill the array with numbers from 2 to 8. For example, assume that array nums is as shown below.
Index
0
1
2
3
4
5
6
Value
6
3
4
5
2
7
3
Index
0
1
2
3
4
5
6
Value
6
3
4
5
2
7
3
Write method countEvens which returns the number of even integers found in its array parameter. Fill the array with numbers from 2 to 8. For example, assume that array nums is as shown belowExplanation / Answer
Hi,
This is a possible solution to your problem. If uses a counter and a loop to go through the array. If the remainder of the number in the current element is 0 when it is divided by 2 then it will add 1 unit to the counter. Finally the method returns the counter which is the number of even numbers in your array.
public int countEvens(int[] nums){
int counter=0;
for(int x=0; x<nums.length; x++){
if((nums[x] % 2) ==0){
counter++;
}
}
return counter;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.