Javatpoint Logo
Javatpoint Logo

Program to find the frequency of odd & even numbers in the given Matrix

Explanation

In this program, we need to find the frequencies of odd and even numbers present in the matrix.

Program to find the frequency of odd & even numbers in the given 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

  1. Declare and initialize a two-dimensional array a.
  2. Calculate the number of rows and columns present in the array a and store it in variables rows and cols respectively.
  3. Maintain two variables countEven and countOdd to store the frequencies of even and odd numbers respectively.
  4. 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.
    1. Check if the element is divisible by 2, if yes then increment the value of countEven by 1.
    2. If the element is an odd number, then increment the value of countOdd by 1.
  5. Finally, display the frequencies of odd and even numbers.

Solution

Python

Output:

Frequency of odd numbers: 5
Frequency of even numbers: 4

C

Output:

Frequency of odd numbers: 5
Frequency of even numbers: 4

JAVA

Output:

Frequency of odd numbers: 5
Frequency of even numbers: 4

C#

Output:

Frequency of odd numbers: 5
Frequency of even numbers: 4

PHP

Output:

Frequency of odd numbers: 5
Frequency of even numbers: 4

Next Topic#




Please Share

facebook twitter google plus pinterest

B.Tech / MCA


Learn Latest Tutorials