Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,788 questions

51,694 answers

573 users

How to check whether a matrix is a square matrix in Java

1 Answer

0 votes
public class SquareMatrixCheck {

    public static boolean isSquareMatrix(int[][] matrix) {
        // Check if the number of rows equals the number of columns
        int rows = matrix.length; // Number of rows
        for (int[] row : matrix) {
            if (row.length != rows) {
                return false; // If any row's length is not equal to the number of rows = not square
            }
        }
        return true; // All rows have the same length as the number of rows
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {5, 8, 9, 10},
            {1, 4, 6, 13},
            {7, 3, 0, 18},
            {6, 8, 9, 20}
        };

        if (isSquareMatrix(matrix)) {
            System.out.println("The matrix is a square matrix.");
        } else {
            System.out.println("The matrix is not a square matrix.");
        }
    }
}



/*
run:

The matrix is a square matrix.

*/

 



answered Oct 7, 2025 by avibootz
...