Java Program to find Matrix is a Sparse Matrix

Write a Java Program to find Matrix is a Sparse Matrix with an example. Any Java matrix called a Sparse Matrix if it contains a large number of zeros. The math formula to find the Sparse Matrix is Total Zeros >= (rows * columns)/2.

In this Java Sparse Matrix example, we declared a 3 * 3 sp_arr integer matrix. Next, we used for loop to iterate the Matrix. We used the If statement to check for zero within the for loop. Based on the result, we are adding the total. Next, we used the If Else statement to check whether the total zero’s total number is greater than (rows * columns)/2. Based on the result, Java code print the output.

public class SparseMatrix {

	public static void main(String[] args) {	
		int i, j, total = 0;
		
		int[][] sp_arr = {{0, 0, 0}, {0, 1, 0}, {5, 4, 9}, {3, 0, 0}};
		
		int rows = sp_arr.length;
		int columns = sp_arr[0].length;
			
		for(i = 0; i < rows ; i++) {
			for(j = 0; j < columns; j++){
				if(sp_arr[i][j] == 0) {
					total++;
				}
			}
		}	
		if(total > ((rows * columns)/2)) {
			System.out.println("\nMatrix is a Sparse Matrix");
		}
		else {
			System.out.println("\nMatrix is Not a Sparse Matrix");
		}
	}
}
Java Program to find Matrix is a Sparse Matrix 1

This Java Sparse Matrix example code is the same as the above. Here, we manually added the row size and column size. We also changed the array a bit and added 9 to first row and second column.

public class SparseMatrix {

	public static void main(String[] args) {	
		int i, j, total = 0;
		
		int[][] sp_arr = {{0, 9, 0}, {0, 1, 0}, {5, 4, 9}, {3, 0, 0}};
			
		for(i = 0; i < 4 ; i++) {
			for(j = 0; j < 3; j++){
				if(sp_arr[i][j] == 0) {
					total++;
				}
			}
		}	
		if(total > ((4 * 3)/2)) {
			System.out.println("\nMatrix is a Sparse Matrix");
		}
		else {
			System.out.println("\nMatrix is Not a Sparse Matrix");
		}
	}
}
Matrix is Not a Sparse Matrix

Java Program to find Matrix is a Sparse Matrix example 2

This Java Sparse Matrix code is the same as the above. However, this Java Sparse code allows the user to enter the number of rows, columns, and the matrix items.

import java.util.Scanner;

public class SparseMatrix {
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		int i, j, rows, columns, total = 0;
		
		sc= new Scanner(System.in);
		
		System.out.println("\n Enter Sparse Matrix Rows and Columns :  ");
		rows = sc.nextInt();
		columns = sc.nextInt();
		
		int[][] arr = new int[rows][columns];
		
		System.out.println("\n Please Enter the Sparse Matrix Items :  ");
		for(i = 0; i < rows; i++) {
			for(j = 0; j < columns; j++) {
				arr[i][j] = sc.nextInt();
			}		
		}
		
		for(i = 0; i < rows ; i++)
		{
			for(j = 0; j < columns; j++)
			{
				if(arr[i][j] == 0) {
					total++;
				}
			}
		}
		
		if(total > ((rows * columns)/2)) {
			System.out.println("\nMatrix is a Sparse Matrix");
		}
		else {
			System.out.println("\nMatrix is Not a Sparse Matrix");
		}
	}
}

Java Sparse Matrix output

 Enter Sparse Matrix Rows and Columns :  
3 3

 Please Enter the Sparse Matrix Items :  
10 20 0
0 0 0
90 0 0

Matrix is a Sparse Matrix

Let me try with less number of zeros.

 Enter Sparse Matrix Rows and Columns :  
3 3

 Please Enter the Sparse Matrix Items :  
1 2 3
0 0 0
5 0 9

Matrix is Not a Sparse Matrix