# Frequencies of even and odd numbers in a matrix

• Difficulty Level : Basic
• Last Updated : 13 Mar, 2023

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```

Implementation:

## CPP

 `// C++ Program to Find the frequency``// of even and odd numbers in a matrix``#include``using` `namespace` `std;` `#define MAX 100` `// function for calculating frequency``void` `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 code``int` `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

 `// Java Program to Find the frequency``// of even and odd numbers in a matrix` `class` `GFG {``static` `final` `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``    ``System.out.print(``" Frequency of odd number ="` `+``                       ``odd + ``" \n"``);``    ``System.out.print(``" Frequency of even number = "` `+``                       ``even + ``" \n"``);``}` `// Driver code``public` `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.`

## Python3

 `# Python Program to Find the frequency``# of even and odd numbers in a matrix` `MAX` `=` `100` `# Function for calculating frequency`  `def` `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 code``m ``=` `3``n ``=` `3` `array ``=` `[[``1``, ``2``, ``3``],``         ``[``4``, ``5``, ``6``],``         ``[``7``, ``8``, ``9``]]` `freq(array, m, n)` `# This code is contributed``# by Anant Agarwal.`

## C#

 `// C# Program to Find the frequency``// of even and odd numbers in a matrix``using` `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

 ``

## Javascript

 ``

Output

``` Frequency of odd number = 5
Frequency of even number = 4 ```

Time Complexity: O(n*m)
Auxiliary Space: O(1), as no extra space is used

## C++

 `// C++ Program to Find the frequency``// of even and odd numbers in a matrix using bitwise &  opeartor``#include ``using` `namespace` `std;` `#define MAX 100` `// function for calculating frequency``void` `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) {``            ``// bitwise & 1 to check``            ``// even and odd``            ``if` `((ar[i][j] & 1) == 0)``                ``++even;``            ``else``                ``++odd;``        ``}``    ``}` `    ``// print Frequency of numbers``    ``cout << ``"Frequency of odd number = "` `<< odd << endl;``    ``cout << ``"Frequency of even number = "` `<< even << endl;``}` `// Driver code``int` `main()``{``    ``int` `m = 3, n = 3;` `    ``int` `array[][MAX]``        ``= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };` `    ``freq(array, m, n);``    ``return` `0;``}` `//This code is contributed by vinay Pinjala.`

## Java

 `import` `java.util.*;` `public` `class` `Main {` `    ``public` `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++) {``                ``// bitwise & 1 to check``                ``// even and odd``                ``if` `((ar[i][j] & ``1``) == ``0``)``                    ``++even;``                ``else``                    ``++odd;``            ``}``        ``}` `        ``// print Frequency of numbers``       ``System.out.println(``"Frequency of odd number = "` `+ odd);``        ``System.out.println(``"Frequency of even number = "` `+ even);``    ``}` `    ``public` `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);``    ``}``}`

## Python3

 `# Python program to find the frequency``# of even and odd numbers in a matrix using bitwise & operator` `# function for calculating frequency``def` `freq(ar, m, n):``    ``even ``=` `0``    ``odd ``=` `0``    ``for` `i ``in` `range``(m):``        ``for` `j ``in` `range``(n):``            ``# bitwise & 1 to check``            ``# even and odd``            ``if``((ar[i][j] & ``1``) ``=``=` `0``):``                ``even ``+``=` `1``            ``else``:``                ``odd ``+``=` `1``    ` `    ``print``(``"Frequency of odd numbers = "``, end ``=` `"")``    ``print``(odd)``    ``print``(``"Frequency of even numbers = "``, end ``=` `"")``    ``print``(even)`  `# driver code``m ``=` `3``n ``=` `3``array ``=` `[[``1``, ``2``, ``3``], [``4``, ``5``, ``6``], [``7``, ``8``, ``9``]]``freq(array, m, n)`

## C#

 `using` `System;` `public` `class` `Program``{``    ``public` `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++)``            ``{``                ``// bitwise & 1 to check even and odd``                ``if` `((ar[i, j] & 1) == 0)``                    ``++even;``                ``else``                    ``++odd;``            ``}``        ``}` `        ``// print Frequency of numbers``        ``Console.WriteLine(``"Frequency of odd number = "` `+ odd);``        ``Console.WriteLine(``"Frequency of even number = "` `+ even);``    ``}` `    ``public` `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);``    ``}``}`

## Javascript

 `// JavaScript program to find the frequency of``// even and odd numbers in a matrix` `// function for calculating frequrency``function` `freq(ar, m, n){``    ``let even = 0;``    ``let odd = 0;``    ``for``(let i = 0; i

Output

```Frequency of odd number = 5
Frequency of even number = 4```

Time Complexity: O(n*m)
Auxiliary Space: O(1), as no extra space is used

This article is contributed by Aarti_Rathi. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up