Write the method called sum of even that take a twodimension

Write the method called sum of even that take a two-dimension away of integers as a parameter and returns the sum of even numbers in the array. For example if the array (as created below) is 10 45 3 8 2 42 3 21 44 the sum of the even number is 10 + 8 + 2 + 44.

Solution

Hi, Please find my code.

Please let me know in case of any issue.

public class Test {

  

   public static int sumOfEven(int[][] arr){

      

       if(arr == null) // if input array is null

           return 0;

      

       int sum = 0;

      

       for(int i=0; i<arr.length; i++){ // row traversal

          

           for(int j=0; j<arr[i].length; j++){ // column wise traversal

              

               if(arr[i][j] % 2 == 0){ // if current value is even

                   sum = sum + arr[i][j];

               }

           }

       }

      

       return sum;

   }

  

   public static void main(String[] args) {

      

       int[][] arr = {

               {10, 45, 3, 8},

               {2, 42},

               {3, 1, 44}

       };

      

       System.out.println(\"Sum of even numbers : \"+sumOfEven(arr));

   }

}

 Write the method called sum of even that take a two-dimension away of integers as a parameter and returns the sum of even numbers in the array. For example if
 Write the method called sum of even that take a two-dimension away of integers as a parameter and returns the sum of even numbers in the array. For example if

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site