We are going to share a Java Program to Check if it is a Sparse Matrix. If you are a Java beginner and want to start learning the Java programming, then keep your close attention in this tutorial as I am going to share how to write a Java Program to Check if it is a Sparse Matrix with the output.
Copy the below Java program and execute it with the help of Javac compiler. At the end of this program, We have shared the output of this program.
public class Sparsity_Matrix
public static void main(String args)
Scanner sc = new Scanner(System.in);
System.out.println("Enter the dimensions of the matrix: ");
int m = sc.nextInt();
int n = sc.nextInt();
double mat = new double[m][n];
int zeros = 0;
System.out.println("Enter the elements of the matrix: ");
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
mat[i][j] = sc.nextDouble();
if(mat[i][j] == 0)
if(zeros > (m*n)/2)
System.out.println("The matrix is a sparse matrix");
System.out.println("The matrix is not a sparse matrix");
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.