TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Find The Frequency of Odd and Even Numbers In The Given Matrix

Program to Find The Frequency of Odd and Even Numbers In The Given Matrix on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

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

#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

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf