Python Program for Frequencies of Even and Odd Numbers in a Matrix

In the previous article, we have discussed Python Program to Check Diagonally Dominant Matrix

Given a square matrix, the task is to get the frequency of even numbers and odd numbers of a given matrix in python.

What is a matrix:

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

Example:

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

Examples:

Example1:

Input:

Given Matrix :
9 0 2
7 5 6
7 1 9

Output:

The odd number frequency of the given matrix = 6
The even number frequency of the given matrix  3

Example2:

Input:

Given Matrix :
5 3 2
8 4 1
7 2 3

Output:

 The odd number frequency of the given matrix = 5
The even number frequency of the given matrix 4

Program for Frequencies of Even and Odd Numbers in a Matrix

Below are the ways to get the frequency of even numbers and odd numbers of a given matrix in python:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the matrix as static input and store it in a variable.
  • Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
  • Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
  • Take a variable to say evnnum_freqcy and initialize its value to 0.
  • Take another variable to say oddnum_freqcy and initialize its value to 0.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Check if the mtrx[n][m] is even using the if conditional statement (where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.).
  • If it is true, then increment the value of above evnnum_freqcy by 1.
  • Else increment the value of above oddnum_freqcy by 1.
  • Print the frequency of even numbers and odd numbers of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Give the matrix as static input and store it in a variable.
mtrx = [[9, 0, 2],
        [7, 5, 6],
        [7, 1, 9]]
# Calculate the number of rows of the given matrix by
# calculating the length of the nested list using the len() function
# and store it in a variable mtrxrows.
mtrxrows = len(mtrx)
# Calculate the number of columns of the given matrix by
# calculating the length of the first list in the nested list
# using the len() function and store it in a variable mtrxcols.
mtrxcols = len(mtrx[0])
# Take a variable to say evnnum_freqcy and initialize its value to 0.
evnnum_freqcy = 0
# Take another variable to say oddnum_freqcy and initialize its value to 0.
oddnum_freqcy = 0
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
  # Inside the For loop, Iterate till the given number of columns using another Nested
  # For loop(Inner For loop).
    for m in range(mtrxcols):
        # Check if the mtrx[n][m] is even using the if conditional statement (where n is the
        # iterator value of the parent For loop and m is the iterator value of the
        # inner For loop.).

        if ((mtrx[n][m] % 2) == 0):
         # If it is true, then increment the value of above evnnum_freqcy by 1.
            evnnum_freqcy += 1
        else:
           # Else increment the value of above oddnum_freqcy by 1.
            oddnum_freqcy += 1
# Print the frequency of even numbers and odd numbers of a given matrix.
print(" The odd number frequency of the given matrix =", oddnum_freqcy)
print(" The even number frequency of the given matrix ", evnnum_freqcy)

Output:

The odd number frequency of the given matrix = 6
 The even number frequency of the given matrix  3

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
  • Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
  • Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
  • Loop till the given number of rows using the For loop
  • Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
  • Add the above row elements list to gvnmatrix using the append() function.
  • Take a variable to say evnnum_freqcy and initialize its value to 0.
  • Take another variable to say oddnum_freqcy and initialize its value to 0.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Check if the mtrx[n][m] is even using the if conditional statement (where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.).
  • If it is true, then increment the value of above evnnum_freqcy by 1.
  • Else increment the value of above oddnum_freqcy by 1.
  • Print the frequency of even numbers and odd numbers of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows of the matrix as user input using the int(input()) function
# and store it in a variable.
mtrxrows = int(input('Enter some random number of rows of the matrix = '))
# Give the number of columns of the matrix as user input using the int(input()) function
# and store it in another variable.
mtrxcols = int(input('Enter some random number of columns of the matrix = '))
# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
mtrx = []
# Loop till the given number of rows using the For loop
for n in range(mtrxrows):
    # Inside the For loop, Give all the row elements of the given Matrix as a list using
    # the list(),map(),int(),split() functions and store it in a variable.
    l = list(map(int, input(
        'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))
    # Add the above row elements list to gvnmatrix using the append() function.

    mtrx.append(l)

# Take a variable to say evnnum_freqcy and initialize its value to 0.
evnnum_freqcy = 0
# Take another variable to say oddnum_freqcy and initialize its value to 0.
oddnum_freqcy = 0
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
  # Inside the For loop, Iterate till the given number of columns using another Nested
  # For loop(Inner For loop).
    for m in range(mtrxcols):
        # Check if the mtrx[n][m] is even using the if conditional statement (where n is the
        # iterator value of the parent For loop and m is the iterator value of the
        # inner For loop.).

        if ((mtrx[n][m] % 2) == 0):
         # If it is true, then increment the value of above evnnum_freqcy by 1.
            evnnum_freqcy += 1
        else:
           # Else increment the value of above oddnum_freqcy by 1.
            oddnum_freqcy += 1
# Print the frequency of even numbers and odd numbers of a given matrix.
print(" The odd number frequency of the given matrix =", oddnum_freqcy)
print(" The even number frequency of the given matrix ", evnnum_freqcy)

Output:

Enter some random number of rows of the matrix = 3
Enter some random number of columns of the matrix = 3
Enter {3} elements of row {1} separated by spaces = 5 3 2
Enter {3} elements of row {2} separated by spaces = 8 4 1
Enter {3} elements of row {3} separated by spaces = 7 2 3
The odd number frequency of the given matrix = 5
The even number frequency of the given matrix 4

Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.