Java Program for Frequencies of even and odd numbers in a matrix Last Updated : 18 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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! Comment More infoAdvertise with us Next Article Java Program for Frequencies of even and odd numbers in a matrix kartik Follow Improve Article Tags : Java Matrix Java Programs Computer Science Fundamentals DSA +1 More Practice Tags : JavaMatrix Similar Reads Java Program to Find the Frequency of Odd & Even Numbers in the Matrix Matrix is simply a two-dimensional array. Just like in one-dimensional traversal operation is required to figure out. The task is to find the frequencies of odd and even number in the given matrix. The size of matrix is defined as product of numbers of columns and rows. So, the size of the matrix is 3 min read Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve 3 min read Java Program for Counting sets of 1s and 0s in a binary matrix Given a n à m binary matrix, count the number of sets where a set can be formed one or more same values in a row or column. Examples: Input: 1 0 1 0 1 0 Output: 8 Explanation: There are six one-element sets (three 1s and three 0s). There are two two- element sets, the first one consists of the first 3 min read Java Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples: Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6 2 min read Java Program to Check if a Given Integer is Odd or Even A number that is divisible by 2 and generates a remainder of 0 is called an even number. All the numbers ending with 0, 2, 4, 6, and 8 are even numbers. On the other hand, number that is not divisible by 2 and generates a remainder of 1 is called an odd number. All the numbers ending with 1, 3, 5,7, 7 min read Like