Explanation
In this program, we need to find the frequencies of odd and even numbers present in the matrix.
In the above example, all odd numbers are represented by the blue square and even numbers are represented by red circles. To find the frequencies of odd and even numbers, loop through the array and check if the element of the array is divisible by 2. If it is divisible by 2(even) then, increment the count of countEven by 1. Else, increment the countOdd by 1.
Algorithm
- Declare and initialize a two-dimensional array a.
- Calculate the number of rows and columns present in the array a and store it in variables rows and cols respectively.
- Maintain two variables countEven and countOdd to store the frequencies of even and odd numbers respectively.
- Two loops will be used to traverse the array where outer loop represents rows, and inner loop represents the columns present in the matrix a.
- Check if the element is divisible by 2, if yes then increment the value of countEven by 1.
- If the element is an odd number, then increment the value of countOdd by 1.
- Finally, display the frequencies of odd and even numbers.
Input:
Matrix a = [4, 1, 3]
[3, 5, 7]
[8, 2, 6]
Output:
Frequency of odd numbers: 5
Frequency of even numbers: 4
Python
Output:
C
Output:
JAVA
Output:
C#
Output:
PHP
Output: