Frequencies of even and odd numbers in a matrix - GeeksforGeeks

29 Apr, 2021
Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples:  
Input : m = 3, n = 3
        { 1, 2, 3 }, 
        { 4, 5, 6 }, 
        { 7, 8, 9 }
Output : Frequency of odd number =  5 
         Frequency of even number = 4


Input :   m = 3, n = 3
         { 10, 11, 12 },
         { 13, 14, 15 },
         { 16, 17, 18 }
Output : Frequency of odd number  =  4 
         Frequency of even number  = 5
 
 
CPP
Java
Python3
C#
PHP
Javascript
// C++ Program to Find the frequency// of even and odd numbers in a matrix#include<bits/stdc++.h>using namespace std; #define MAX 100 // function for calculating frequencyvoid freq(int ar[][MAX], int m, int n){    int even = 0, odd = 0;         for (int i = 0; i < m; ++i)    {        for (int j = 0; j < n; ++j)        {            // modulo by 2 to check            // even and odd            if ((ar[i][j] % 2) == 0)                ++even;            else                ++odd;        }    }         // print Frequency of numbers    printf(" Frequency of odd number = %d \n", odd);    printf(" Frequency of even number = %d \n", even);} // Driver codeint main(){    int m = 3, n = 3;            int array[][MAX] = { { 1, 2, 3 },                        { 4, 5, 6 },                        { 7, 8, 9 } };     freq(array, m, n);    return 0;}       
// Java Program to Find the frequency// of even and odd numbers in a matrix class GFG {static final int MAX = 100; // function for calculating frequencystatic void freq(int ar[][], int m, int n) {    int even = 0, odd = 0;     for (int i = 0; i < m; ++i)    {        for (int j = 0; j < n; ++j)        {            // modulo by 2 to check            // even and odd            if ((ar[i][j] % 2) == 0)                ++even;            else                ++odd;    }    }     // print Frequency of numbers    System.out.print(" Frequency of odd number =" +                       odd + " \n");    System.out.print(" Frequency of even number = " +                       even + " \n");} // Driver codepublic static void main(String[] args) {    int m = 3, n = 3;     int array[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};     freq(array, m, n);}}// This code is contributed by Anant Agarwal.
# Python Program to Find the frequency# of even and odd numbers in a matrix MAX=100  # Function for calculating frequencydef freq(ar, m, n):    even = 0    odd = 0          for i in range(m):        for j in range(n):                     # modulo by 2 to check            # even and odd            if ((ar[i][j] % 2) == 0):                even += 1            else:                odd += 1          # print Frequency of numbers    print(" Frequency of odd number =", odd)    print(" Frequency of even number =", even)   # Driver codem = 3n = 3         array = [ [ 1, 2, 3 ],        [ 4, 5, 6 ],        [ 7, 8, 9 ] ]  freq(array, m, n) # This code is contributed# by Anant Agarwal.
// C# Program to Find the frequency// of even and odd numbers in a matrixusing System; class GFG{    //static int MAX = 100;         // function for calculating frequency    static void freq(int [,]ar, int m, int n)    {        int even = 0, odd = 0;             for (int i = 0; i < m; ++i)        {            for (int j = 0; j < n; ++j)            {                // modulo by 2 to check                // even and odd                if ((ar[i, j] % 2) == 0)                    ++even;                else                    ++odd;        }        }             // print Frequency of numbers        Console.WriteLine(" Frequency of odd number =" +                        odd );        Console.WriteLine(" Frequency of even number = " +                        even );    }         // Driver code    public static void Main()    {        int m = 3, n = 3;             int [,]array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};             freq(array, m, n);    }}// This code is contributed by vt_m.
<?php// PHP Program to Find the frequency// of even and odd numbers in a matrix$MAX = 100; // function for calculating frequencyfunction freq($ar, $m, $n){    $even = 0; $odd = 0;         for($i = 0; $i < $m; ++$i)    {        for ( $j = 0; $j < $n; ++$j)        {            // modulo by 2 to check            // even and odd            if (($ar[$i][$j] % 2) == 0)                ++$even;            else                ++$odd;        }    }         // print Frequency of numbers    echo " Frequency of odd number = "                           , $odd,"\n";    echo " Frequency of even number = "                               , $even;}     // Driver code    $m = 3; $n = 3;    $array = array(array(1, 2, 3),                   array(4, 5, 6),                   array(7, 8, 9));    freq($array, $m, $n); // This code is contributed by anuj_67.?>
<script>// Java Script Program to Find the frequency// of even and odd numbers in a matrix let MAX = 100; // function for calculating frequencyfunction freq(ar,m,n) {    let even = 0, odd = 0;     for (let i = 0; i < m; ++i)    {        for (let j = 0; j < n; ++j)        {                                     // even and odd            if ((ar[i][j] % 2) == 0)                ++even;            else                ++odd;    }    }     // print Frequency of numbers    document.write(" Frequency of odd number =" +                    odd + " <br>");    document.write(" Frequency of even number = " +                    even + "<br>");} // Driver code     let m = 3, n = 3;     let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];     freq(array, m, n); // This code is contributed by sravan kumar G</script>
Output:  
 Frequency of odd number = 5  
 Frequency of even number = 4
 
vt_m
sravankumar8128
Matrix
School Programming
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
Unique paths in a Grid with Obstacles
Mathematics | L U Decomposition of a System of Linear Equations
C program to implement Adjacency Matrix of a given Graph
Java Program to Multiply two Matrices of any size
Python Dictionary
Inheritance in C++
Arrays in C/C++
Overriding in Java
Copy Constructor in C++