# Frequencies of even and odd numbers in a matrix

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

`// 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 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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to Find the frequency ` `// of even and odd numbers in a matrix ` `$MAX` `= 100; ` ` ` `// function for calculating frequency ` `function` `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. ` `?> ` |

*chevron_right*

*filter_none*

Output:

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Place N^2 numbers in matrix such that every row has an equal sum
- Print numbers in matrix diagonal pattern
- Count of numbers in given range L to R which are present in a Matrix
- Minimum cost to sort a matrix of numbers from 0 to n^2 - 1
- Arrange numbers 1 to N^2 in a Zig-Zag Matrix in ascending order
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Program to check diagonal matrix and scalar matrix
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Convert given Matrix into sorted Spiral Matrix
- Maximize sum of N X N upper left sub-matrix from given 2N X 2N matrix
- C++ program to Convert a Matrix to Sparse Matrix
- Program to convert given Matrix to a Diagonal Matrix
- Check if it is possible to make the given matrix increasing matrix or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.