Thursday, December 11, 2025
HomeLanguagesJavaJava Program for Frequencies of even and odd numbers in a matrix

Java Program for 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

 

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 + "
");
    System.out.print(" Frequency of even number = " +
                       even + "
");
}
 
// 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.


Output: 
 

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

Time Complexity: O(m*n)

Auxiliary Space: O(1)

Method: Using bitwise & operator

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)
        {
            // bitwise & 1 to check
            // even and odd
            if ((ar[i][j] &1) == 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 + " ");
}
 
// 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 tvsk.


Output

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

Time Complexity: O(m*n)

Auxiliary Space: O(1)

Please refer complete article on Frequencies of even and odd numbers in a matrix for more details!

RELATED ARTICLES

Most Popular

Dominic
32440 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6811 POSTS0 COMMENTS
Nicole Veronica
11950 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12023 POSTS0 COMMENTS
Shaida Kate Naidoo
6943 POSTS0 COMMENTS
Ted Musemwa
7193 POSTS0 COMMENTS
Thapelo Manthata
6889 POSTS0 COMMENTS
Umr Jansen
6880 POSTS0 COMMENTS