Signup/Sign In
LAST UPDATED: MARCH 5, 2021

Java Program to find the Frequency of Odd and Even Numbers in a Matrix

In this tutorial, we will learn how to find the frequency of odd and even numbers in a matrix. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Below is the pictorial representation of a matrix.

Input: Enter the matrix element:

1 2 3

4 3 2

6 7 8

Output:

Even element frequency: 5

Odd element frequency: 4

Program 1: Find the Frequency of Odd and Even Numbers in a Matrix

In the below program, we will see how to calculate the frequency of odd and even numbers in a given matrix when the values are user-defined.

Algorithm

  1. Start
  2. Declare variables for matrix rows and columns.
  3. Ask the user to initialize the matrix.
  4. Declare the matrix.
  5. Ask the user to initialize the matrix.
  6. Print the original matrix..
  7. Declare two variables to count the even and odd number frequencies.
  8. Initialize these variables to 0.
  9. Use two for loops to iterate through the elements.
  10. Use the first for loop to iterate through the rows.
  11. Use the second for loop to iterate through the columns.
  12. Check for each element.
  13. Increment the even count variable if the matrix element is even.
  14. Increment the odd count variable if the matrix element is odd.
  15. Print the frequency for both the even and odd elements in the array.
  16. Stop.

Below is the code for the same.

/* Java Program to check the even and odd element frequencies*/
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args) 
    {
        int m,n;   //Declare the variables for rows and columns

        //Take input from the user
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter number of rows in matrix:");
        m = sc.nextInt();  //Initialize the number of rows

        System.out.print("Enter number of columns in matrix:");
        n = sc.nextInt();  //Initialize the number of columns
 
        int arr[][] = new int[m][n];  //Declare a Matrix
        System.out.println("Enter all the elements of matrix:");
        for (int i = 0; i < m; i++) 
        {
            for (int j = 0; j < n; j++) 
            {
                arr[i][j] = sc.nextInt();     //Initialize the Matrix
            }
        }
        
        //Print the original Matrix
        System.out.println("The Original Matrix:");
        for (int i = 0; i < m; i++)      //Used to iterate over the matrix rows
        {
            for (int j = 0; j < n; j++)    //Used to iterate over the matrix columns
            {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println("");
        }
        
        int even=0,odd=0;   //Variables to store even and odd elements
        
        //Use for loops to iterate through the matrix elements
        for(int i=0;i<m;i++)      //Used to iterate over the matrix rows
        {
            for(int j=0;j<n;j++)   //Used to iterate over the matrix columns
            {
                if(arr[i][j]%2==0)     //Check whether the element is even or not
                {
                    even++;      //Increment the even frequency
                }
                else
                {
                    odd++;      //Increment the odd frequency
                }
            }
        }
        System.out.println("Total Odd Number in the Matrix: " + odd); 
        System.out.println("Total Even Number in the Matrix: " + even); 
         
    }
}


Enter number of rows in matrix:3
Enter number of columns in matrix:3
Enter all the elements of matrix: 1 2 3 4 5 6 7 8 9
The Original Matrix:
1 2 3
4 5 6
7 8 9
Total Odd Number in the Matrix: 5
Total Even Number in the Matrix: 4

Program 2: Find the Frequency of Odd and Even Numbers in a Matrix

In the below program, we will see how to calculate the frequency of odd and even numbers in a given matrix when the values are pre-defined.

Algorithm

  1. Start
  2. Declare a matrix and initialize it to 0.
  3. Call a method to count the even and odd frequency.
  4. Use a for loop to iterate over the elements.
  5. Increment the even count each time an even element encounters.
  6. Increment the odd count each time an odd element encounters.
  7. Print the frequency for both the even and odd elements in the array.
  8. Stop

Below is the code for the same.

/*Java Program to find the trace and normal of a matrix*/
import java.io.*; 
  
public class Main 
{
    
    //To Find the normal of a matrix 
    public static void findNormal(int[][] arr) 
    { 
         double square = 0, result = 0;
        System.out.println("The Normal of the above matrix is "); 
   	for(int i = 0; i < arr.length; i++)
   	{
    	    for(int j = 0; j < arr[0].length; j++)
       	    {
       	        square = square + (arr[i][j])*(arr[i][j]);
            }
    	}
        result = Math.sqrt(square);
        System.out.println(result);
    } 
    
    //To Find the trace of a matrix 
    public static void findTrace(int[][] arr) 
    { 
        double sum = 0;
        System.out.println("The Trace of the above matrix is ");
  	for(int i = 0; i < arr.length; i++)
  	{  
    	    for(int j = 0; j < arr[0].length; j++)
       	    {
                if(i == j)
            	 {
               	     sum = sum + (arr[i][j]);
               	 }
            }
        }
        System.out.println(sum); 
         
    } 
    
    
      
    // Driver code 
    public static void main(String args[]) throws IOException 
    { 
        int arr[][] 
            = { { 2, 9, 8 }, { 7, 6, 4 }, { 3, 9, 2 } };  //Matrix Declaration and Initialization
       
       System.out.println("Original Matrix");
       for(int i = 0; i < arr.length; i++)
  	   {  
    	    for(int j = 0; j < arr[0].length; j++)
       	    {
                System.out.print(arr[i][j]+ " ");
            }
            System.out.println();
        }
        System.out.println();
        findTrace(arr);    //Find the Trace of the Matrix
        System.out.println();
        findNormal(arr);   //Find the Normal of the Matrix
           
    } 
} 


Original Matrix
2 9 8
7 6 4
3 9 2
The Trace of the above matrix is
10.0
The Normal of the above matrix is
18.547236990991408



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.