Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 below

Explanation / 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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote