# Java Program to Check Whether a Given Matrix is a Sparse Matrix

In the previous article, we have discussed Java Program to Find the Transpose of a Given Matrix

In this article we are going to see how we can write a program to check if a matrix is a sparse matrix in JAVA language.

## Java Program to Check Whether a Given Matrix is a Sparse Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
| A20   A21   A22 | 3*3
• Matrix A represents a 3*3 matrix.
• A‘ represents the matrix element
• Aij‘ represents the matrix element at it’s matrix position/index.
• i‘ represents the row index
• j‘ represents the column index
• Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
• Here we have started row value from 0 and column value from 0.

Sparse matrix is a matrix which has more number of zeroes than non-zero elements.

For example:

Matrix :

2 0 0
0 9 0
0 0 6

As the number of zero elements are more than number of non zero elements so it is a sparse matrix.

Let’s see different ways to check Check Whether a Given Matrix is a Sparse Matrix.

### Method-1: Java Program to Check Whether a Given Matrix is a Sparse Matrix By Static Initialization of Array Elements

Approach:

• Initialize and declare an array with elements.
• Using two for loops to iterate the rows and columns. Then count the number of zero elements inside the matrix.
• If the number of zero elements are greater than half the number of elements in the matrix, then it is a sparse matrix, else not .

Program:

public class matrix{
public static void main(String args[])
{
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = {{0,5,0},{0,0,6},{7,0,6}};
int row, col ,counter = 0;

System.out.print("The matrix elements are : ");
printMatrix(arr);

// Loops to find transpose of the matrix
for(row=0;row<3;row++)
for(col=0;col<3;col++)
{
if(arr[row][col]==0)
counter++;
}
// Checks if counter is greater than hlaf of total elements in the matrix
// 9 is the number of elements here
if(counter>9/2)
System.out.println("\nIt is a sparse matrix");
else
System.out.println("\nIt is not a sparse matrix");
}

// Function to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}
}


Output:

The matrix elements are :
0 5 0
0 0 6
7 0 6
It is a sparse matrix

### Method-2: Java Program to Check Whether a Given Matrix is a Sparse Matrix By Dynamic Initialization of Array Elements

Approach:

• Declare two arrays of size 3×3.
• Ask the user for input and store them in the arrays using two for loops.
• Using two for loops to iterate the rows and columns. Then count the number of zero elements inside the matrix.
• If the number of zero elements are greater than half the number of elements in the matrix, then it is a sparse matrix, else not.

Program:

import java.util.Scanner;
public class matrix
{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);

// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = new int[3][3];
int row, col ,counter = 0;

// Taking matrix input
System.out.println("\nEnter the first matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] = scan.nextInt();

System.out.print("The matrix elements are:");
printMatrix(arr);

// Loops to find transpose of the matrix
for(row=0;row<3;row++)
for(col=0;col<3;col++)
{
if(arr[row][col]==0)
counter++;
}
// Checks if counter is greater than hlaf of total elements in the matrix
// 9 is the number of elements here
if(counter>9/2)
System.out.println("\nIt is a sparse matrix");
else
System.out.println("\nIt is not a sparse matrix");
}

// Function to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}
}


Output:

Case-1
Enter the first matrix elements : 0 2 0 7 0 0 3 4 0
The matrix elements are:
0 2 0
7 0 0
3 4 0

It is a sparse matrix

Case-2

Enter the first matrix elements :
The matrix elements are:
41 22 38
91 25 71
83 44 51

It is not a sparse matrix

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: