# Java Program to Check Sum of n-th row and n-th column are Equal

In the previous article, we have seen Java Program to Increment the Matrix Elements by One

In this article we are going to see how we can write a program we will check if the row sum is equal to the column sum in the matrix in JAVA language.

## Java Program to Check Sum of n-th row and n-th column are Equal

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
| A20   A21   A22 | 3*3
• Matrix A represents a 3*3 matrix.
• A‘ represents the matrix element
• Aij‘ represents the matrix element at it’s matrix position/index.
• i‘ represents the row index
• j‘ represents the column index
• Means A00=Aij  where i=0 and j=0,  A01=aij where i=0 and j=1 and like this.
• Here we have started row value from 0 and column value from 0.

Let’s see different ways Check Sum of n-th row and n-th column are Equal.

### Method-1: Java Program to Check Sum of n-th row and n-th column are Equal By Static Initialization of Array Elements

Approach:

• Initialize and declare two arrays one of size 3×3 with elements and another 1D array of size 3.
• Ask the user to enter an index.
• Take for loops to iterate the row and column index.
• Add the sum of rows and columns of the entered index and check.
• Print the result.

Program:

import java.util.Scanner;
public class Matrix
{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);

// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = {{10,20,30},{40,50,60},{30,60,90}};
int row, col ;

System.out.print("The matrix elements are:");
printMatrix(arr);
System.out.println("\nEnter nth row-column index to check : ");
int n = scan.nextInt();
if(rowColumnSum(arr,n))
{
System.out.print("\nRows and column sums are same");
}
else
System.out.print("\nRows and column sums are not same");

}

// Function to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}

// Check if the sums are equal or not
static boolean rowColumnSum(int arr[][], int n)
{
int rowSum=0, colSum=0, row, col;

//calculates rowSum
for(col=0;col<3;col++)
rowSum+=arr[n][col];
System.out.println("Row"+n+"sum:"+rowSum);

//calculates colSum
for(row=0;row<3;row++)
colSum+=arr[row][n];
System.out.println("Column"+n+"sum:"+colSum);

if(rowSum==colSum)
return true;
else
return false;
}
}


Output:

The matrix elements are:
10 20 30
40 50 60
30 60 90
Enter nth row-column index to check
Row 2 sum:180
Column 2 sum:180

Rows and column sums are same

### Method-2: Java Program to Check Sum of n-th row and n-th column are Equal By Dynamic Initialization of Array Elements

Approach:

• Declare two arrays one of size 3×3 and another 1D array of size 3.
• Ask the user for input of array elements and store them in the one array using two for loops.
• Ask the user to enter an index.
• Take for loops to iterate the row and column index.
• Add the sum of rows and columns of the entered index and check.
• Print the result.

Program:

import java.util.Scanner;
public class Matrix
{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);

// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = new int[3][3];
int row, col ;

// Taking matrix input
System.out.println("\nEnter matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] = scan.nextInt();

System.out.print("The matrix elements are:");
printMatrix(arr);
System.out.println("\nEnter nth row-column index to check : ");
int n = scan.nextInt();
if(rowColumnSum(arr,n))
{
System.out.print("\nRows and column sums are same");
}
else
System.out.print("\nRows and column sums are not same");

}

// Function to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}

// Check if the sums are equal or not
static boolean rowColumnSum(int arr[][], int n)
{
int rowSum=0, colSum=0, row, col;

//calculates rowSum
for(col=0;col<3;col++)
rowSum+=arr[n][col];
System.out.println("Row "+n+" sum:"+rowSum);

//calculates colSum
for(row=0;row<3;row++)
colSum+=arr[row][n];
System.out.println("Column "+n+" sum:"+colSum);

if(rowSum==colSum)
return true;
else
return false;
}
}


Output:

Case-1

Enter matrix elements : 10 20 30 40 50 60 7080 90
The matrix elements are:
10 20 30
40 50 60
70 80 90
Enter nth row-column index to check : 1
Row 1 sum:150
Column 1 sum:150

Rows and column sums are same

Case-2

Enter matrix elements : 10 90 30 40 50 60 70 80 90
The matrix elements are:
10 90 30
40 50 60
70 80 90
Enter nth row-column index to check : 1
Row 1 sum:150
Column 1 sum:220

Rows and column sums are not same

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Related Java Programs: