public class FrequencyOfOddEvenNumbers {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
findFrequency(matrix);
}
public static void findFrequency(int[][] matrix) {
int oddCount = 0;
int evenCount = 0;
for (int[] row : matrix) {
for (int num : row) {
if (num % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
}
System.out.println("Frequency of even numbers: " + evenCount);
System.out.println("Frequency of odd numbers: " + oddCount);
}
}
Frequency of even numbers: 4
Frequency of odd numbers: 5
matrix
represented as a two-dimensional array. We define a method findFrequency
that takes the matrix as input and finds the frequency of odd and even numbers in the matrix.oddCount
and evenCount
to zero to keep track of the frequency of odd and even numbers in the matrix. We then use two nested loops to iterate over the elements of the matrix. The outer loop iterates over the rows, and the inner loop iterates over the columns.evenCount
variable. Otherwise, we increment the oddCount
variable.