 # PHP Program to count of the odd and even numbers in the matrix

## How do we get the count of the odd and even numbers in the matrix?

In this program, we are counting the odd and even numbers in the given matrix. Odd numbers are those numbers when operate by Mod(%) 2 and the remainder will be non 0 and when the remainder will be 0 then the number will be an Even number. For example, if the entered number is 8 and when '8 % 2' is calculated and the result will be 0 so it is an even number, and if the entered number is 7 and when '7 % 2' is calculated the result will be 1 so it is an odd number. After finding the odd and even we assign them in different arrays.

## How do we get the count of the odd and even numbers in the matrix using PHP?

In this program, we are using the static values which are initialized in the code itself. First, we have to initialize two-dimensional arrays a[] then we have to assign the number of rows and columns into the variable row and col using the built-in function `count(). `After that, we have to create variables odd and even with value 0 to store the count of the odd numbers and even numbersThen assign the value 0 into the variable i and perform the loop until the condition 'i < row' becomes false and increment the value of variable i in every iteration in the block of the loop we have to perform another loop in that we have to assign the value 0 into the variable j and perform the loop until the condition 'j < col' becomes false and increment the value of variable j in every iteration in the loop block we have to check the condition 'a1[i][j] % 2 == 0' if true increment the value of the variable even otherwise increment the value of the variable odd.

### ALGORITHM

Step 1: Initialize two-dimensional arrays a[]

Step 2: Assign the number of rows and columns into the variable row and col using the built-in function `count()`

Step 3: Print the elements in the array a[]

Step 4: Create variables odd and even with value 0 to store the count of odd and even numbers in the matrix

Step 5: Assign the value 0 into the variable i and perform the sub-step until the condition 'i < row' becomes false and increment the value of variable i in every iteration

(i) Assign the value 0 into the variable j and perform the sub-step until the condition 'j < col' becomes false and increment the value of variable j in every iteration

(a) Check the condition 'a[i][j] % 2 == 0' if true increment the value of the variable even otherwise increment the value of the variable odd

Step 6: Print the values of the variables even and odd as the count of the even number and odd number

## PHP Source Code

```                                          ```<?php
\$a = array(
array(6, 7, 5),
array(3, 5, 1),
array(8, 3, 4)
);

\$row = count(\$a);
\$col = count(\$a);
\$odd = 0;
\$even = 0;
echo "The matrix: \n";
for (\$i = 0; \$i < \$row; \$i++) {
for (\$j = 0; \$j < \$col; \$j++) {
echo \$a[\$i][\$j] . " ";
}
echo "\n";
}
for (\$i = 0; \$i < \$row; \$i++) {
for (\$j = 0; \$j < \$col; \$j++) {
if (\$a[\$i][\$j] % 2 == 0) {
\$even++;
} else {
\$odd++;
}
}
}
echo "\nCount of even numbers in the matrix is: \$even";
echo "\nCount of odd numbers in the matrix is: \$odd";
?>```
```

## OUTPUT

```The matrix:
6 7 5
3 5 1
8 3 4

Count of even numbers in the matrix is: 3
Count of odd numbers in the matrix is: 6```
VIEW ALL
VIEW ALL
VIEW ALL