Q:

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

belongs to collection: Matrix Programs

0

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

  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.

Input:

 

Matrix a = [4, 1, 3]  

           [3, 5, 7]  

           [8, 2, 6]  

Output:

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

#Initialize matrix a  
a = [    
        [4, 1, 3],  
        [3, 5, 7],  
        [8, 2, 6]  
    ];  
   
countOdd = 0;  
countEven = 0;  
   
#Calculates number of rows and columns present in given matrix  
rows = len(a);  
cols = len(a[0]);  
   
#Counts the number of even elements and odd elements  
for i in range(0, rows):  
    for j in range(0, cols):  
        if(a[i][j] % 2 == 0):  
            countEven = countEven + 1;  
        else:  
            countOdd = countOdd + 1;  
   
print("Frequency of odd numbers: " + str(countOdd));  
print("Frequency of even numbers: " + str(countEven));  

 

Output:

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

 

C

#include <stdio.h>  
   
int main()  
{  
    int rows, cols, countOdd = 0, countEven = 0;  
          
    //Initialize matrix a  
    int a[][3] = {     
                    {4, 1, 3},  
                    {3, 5, 7},  
                    {8, 2, 6}  
                };  
      
    //Calculates number of rows and columns present in given matrix  
    rows = (sizeof(a)/sizeof(a[0]));  
    cols = (sizeof(a)/sizeof(a[0][0]))/rows;  
      
    //Counts the number of even elements and odd elements  
    for(int i = 0; i < rows; i++){  
        for(int j = 0; j < cols; j++){  
          if(a[i][j] % 2 == 0)  
            countEven++;  
          else  
            countOdd++;  
        }  
    }  
      
    printf("Frequency of odd numbers: %d\n", countOdd);  
    printf("Frequency of even numbers: %d\n", countEven);  
          
    return 0;  
}  

 

Output:

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

 

JAVA

public class OddEven  
{  
    public static void main(String[] args) {  
        int rows, cols, countOdd = 0, countEven = 0;  
          
        //Initialize matrix a  
        int a[][] = {     
                        {4, 1, 3},  
                        {3, 5, 7},  
                        {8, 2, 6}  
                    };  
            
          //Calculates number of rows and columns present in given matrix  
          rows = a.length;  
        cols = a[0].length;  
          
        //Counts the number of even elements and odd elements  
        for(int i = 0; i < rows; i++){  
            for(int j = 0; j < cols; j++){  
              if(a[i][j] % 2 == 0)  
                countEven++;  
              else  
                countOdd++;  
            }  
        }  
          
        System.out.println("Frequency of odd numbers: " + countOdd);  
        System.out.println("Frequency of even numbers: " + countEven);  
    }  
}  

 

Output:

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

 

C#

using System;  
                      
public class OddEven  
{  
    public static void Main()  
    {  
        int rows, cols, countOdd = 0, countEven = 0;  
          
        //Initialize matrix a  
        int[,] a = {     
                        {4, 1, 3},  
                        {3, 5, 9},  
                        {8, 2, 6}  
                   };  
            
          //Calculates number of rows and columns present in given matrix  
          rows = a.GetLength(0);  
        cols = a.GetLength(1);  
          
        //Counts the number of even elements and odd elements  
        for(int i = 0; i < rows; i++){  
            for(int j = 0; j < cols; j++){  
              if(a[i,j] % 2 == 0)  
                countEven++;  
              else  
                countOdd++;  
            }  
        }  
          
        Console.WriteLine("Frequency of odd numbers: " + countOdd);  
        Console.WriteLine("Frequency of even numbers: " + countEven);  
    }  
}  

 

Output:

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

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
//Initialize matrix a  
$a = array(     
         array(4, 1, 3),  
         array(3, 5, 7),  
         array(8, 2, 6)  
     );  
   
$countEven = 0;  
$countOdd = 0;  
      
//Calculates number of rows and columns present in given matrix  
$rows = count($a);  
$cols = count($a[0]);  
   
//Counts the number of even elements and odd elements  
for($i = 0; $i < $rows; $i++){  
    for($j = 0; $j < $cols; $j++){  
      if($a[$i][$j] % 2 == 0)  
        $countEven++;  
      else  
        $countOdd++;  
    }  
}  
   
print("Frequency of odd numbers: " . $countOdd);  
print("<br>Frequency of even numbers: " . $countEven);  
?>  
</body>  
</html>

  

 

Output:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to find the product of two matrices... >>
<< Program to display the upper triangular matrix...