11:41 pm - Saturday August 18, 2018

Java Example Program to find whether a given Matrix is a square matrix or not

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
/**
* Example Program to check whether the given matrix is a square matrix or not
* @author: candidjava.com
* @description: check whether the given matrix is a square matrix or not
*/

public class Matrix
{

public static void main(String[] args)
{

int num [][] = { {4, 6, 7, 7}, {5, 1, 8, 6}, {1, 0, 3, 10}, {5, 3, 2, 1}};

System.out.println("the array elements are:");

for(int i = 0; i < num.length; i++)
{
System.out.println();

for(int j = 0; j < num[i].length; j++)
System.out.print(num[i][j] + " "); // prints the matrix
}

System.out.println(" Number ofrows=" +num.length); // prints the number of rows
System.out.println("Number of columns=" +num[0].length); //prints the number of columns

if(num.length==num[0].length) // condition to check whether the given matrix is a square matrix or not
{
System.out.println("it is a square matrix");
}
else
{
System.out.println("it is not a square matrix");
}
}
}


Output:
4 6 7 7
5 1 8 6
1 0 3 10
5 3 2 1
Number ofrows=4
Number of columns=4
it is a square matrix

Filed in: Z-A All Java Codes

Comments are closed.