0% found this document useful (0 votes)
3 views2 pages

Losso Matrix Checker in Java

The document outlines an algorithm and Java code for determining if a user-defined square matrix is a Losso Matrix. It includes steps for accepting the matrix dimensions, validating the input, displaying the matrix, and calculating the sums of the left and right diagonals. The program checks if the sums of each row and column match the sum of the first row to determine if the matrix is a Losso Matrix.

Uploaded by

zorolostinsea
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Losso Matrix Checker in Java

The document outlines an algorithm and Java code for determining if a user-defined square matrix is a Losso Matrix. It includes steps for accepting the matrix dimensions, validating the input, displaying the matrix, and calculating the sums of the left and right diagonals. The program checks if the sums of each row and column match the sum of the first row to determine if the matrix is a Losso Matrix.

Uploaded by

zorolostinsea
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ALGORITHM

Step 1: Start a class


Step 2: Declare the DDA A[][] and the variable n of integer type
Step 3: Accept the dimension of the array from the user
Step 4: Check if n<=2 or n>=10 then print a suitable message and go to step 13
Step 5: Initialise the array A[][]
Step 6: start a loop i from 0 to n-1 and loop j from 0 to n-1 to accept the elements in the array
Step 7: Display the original matrix
Step 8: Check whether the matrix is a Losso Matrix by creating a variable test to check the sum of first
row and compare it with the sum of each row and column
Step 9: Print whether Matrix is Losso or not
Step 10: Initialize variable ld and rd in integer type
Step 11: Calculate the sum of Left Diagonal and Right Diagonal
Step 12: Print the sum of Left and Right Diagonal separately
Step 13: end of algorithm.

import [Link].*;
public class Losso
{
int A[][];
int n;
public void getData(){
Scanner sc=new Scanner([Link]);
[Link]("Enter the dimension: "); //Accepts the dimensions of the matrix from
user
n=[Link]();
if(n<=2 || n>=10){ //Check whether the dimensions are valid or not
[Link]("INVALID INPUT");
return;
}

A=new int[n][n]; //initialise the Array

int i=0;
int j=0;
[Link]("Enter the elements of the matrix: "); //Accept the elments of the matrix from
user
for(i=0;i<n;i++){
for(j=0;j<n;j++){
A[i][j]=[Link]();
}
}
}
public void display(){
if(n<=2 || n>=10){ //Check whether the dimensions are valid or not
[Link]("");
return;
}

[Link]("ORIGINAL MATRIX");
int i=0;
int j=0;
for(i=0;i<n;i++){ //Displays the original matrix
for(j=0;j<n;j++){
[Link](A[i][j]+" ");
}
[Link]();
}
if(isLosso()){
[Link]("It is a Losso Grid");
}
else
[Link]("It is not a Losso Grid");

int ld=0;
int rd=0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==j)
ld=ld+A[i][j];
if((i+j)==n-1)
rd=rd+A[i][j];
}
}
[Link]("Left Diagonal: "+ld); //used for calculating the Left Diagonal Sum
[Link]("Right Diagonal: "+rd); //used for calculating the Right Diagoal Sum
}
public boolean isLosso(){ //used to check whether the Matrix is a Losso Matrix or not
int i=0;
int j=0;
int test=0;
int r=0;
int c=0;
int k=0;
for(i=0;i<n;i++){
test=test+A[0][i];
}

for(i=0;i<n;i++){
for(j=0;j<n;j++){
r=r+A[i][j];
}
for(k=0;k<n;k++){
c=c+A[k][i];
}
if(test!=r || test!=c)
return false;
r=0;
c=0;
}
return true;
}
public static void main(String arg[]){
Losso ob=new Losso();
[Link]();
[Link]();
}
}

You might also like