Java Program to Find Difference between Sum of all Rows and Sum of all Columns

In the previous article, we have seen Java Program to Find Sum of all rows and sum of all columns are equal

In this article we are going to see how we can write a program to find difference between sum of all rows and sum of all columns using JAVA language.

Java Program to Find Difference between Sum of all Rows and Sum of all Columns

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=0A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

We are finding sum of all rows and all columns of a matrix. Then we are finding the difference between them. While finding sum of all rows and all columns, means indirectly here we are finding the sum of all the matrix elements. As all rows covers all the elements of matrix similarly all columns also covers all the elements of matrix. So we will always get same result for sum of all rows and sum of all columns. So, the difference will be 0 always.

Let’s see different ways to find difference between sum of all rows and sum of all columns.

Method-1: Java Program to Find Difference between Sum of all Rows and Sum of all Columns By Static Initialization of Array Elements

Approach:

  • Initialize and declare one array with size 3×3 which is for matrix.
  • Use two for loops to iterate the rows and columns .
  • Inside the for loops calculate the row and column sum together.
  • Then find the difference between the sum of all rows and sum of all columns where the difference will be zero.
  • Print the result using a for loop.

Program:

public class Matrix
{
    public static void main(String args[])
    {

        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = {{10,20,30},{40,40,60},{70,80,90}};
        int row, col ;
        //row Sum and colSum initialized as 0
        int rowSum=0;
        int colSum=0;
        

        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        // Loops to calculate the sum of all rows and all columns 
        for(row=0;row<3;row++)
        {
            for(col=0;col<3;col++)
            {
                rowSum = rowSum+arr[row][col];
                colSum = colSum+arr[col][row];
            }   
        }
        
        int diff=rowSum-colSum;
       
       //Printing the difference between sum of all rows and all columns
        System.out.print("\nDifference between Sum of all rows and sum of all columns : "+diff);

    }
    
    //Method 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]+" ");
            }
        }
    }
}
Output:

The matrix elements are : 
10 20 30 
40 40 60 
70 80 90 
Difference between Sum of all rows and sum of all columns : 0

Method-2: Java Program to Find Difference between Sum of all Rows and Sum of all Columns By Dynamic Initialization of Array Elements

Approach:

  • Declare one array with size 3×3 which is for matrix.
  • Ask the user for input of matrix elements.
  • Use two for loops to iterate the rows and columns .
  • Inside the for loops calculate the row and column sum together.
  • Then find the difference between the sum of all rows and sum of all columns where the difference will be zero.
  • Print the result using a for loop.

Program:

import java.util.*;

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 ;
        
        //row Sum and colSum initialized as 0 
        int rowSum=0; 
        int colSum=0;
        
        // Taking matrix1 input
        System.out.println("Enter 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);

        // Loops to calculate the sum of all rows and all columns 
        for(row=0;row<3;row++)
        {
            for(col=0;col<3;col++)
            {
                rowSum = rowSum+arr[row][col];
                colSum = colSum+arr[col][row];
            }   
        }
        
        int diff=rowSum-colSum;
       
       //Printing the difference between sum of all rows and all columns
        System.out.print("\nDifference between Sum of all rows and sum of all columns : "+diff);

    }
    
    //Method 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]+" ");
            }
        }
    }
}
Output:
Enter matrix elements : 1 1 1 1 1 1 1 1 1 1 
The matrix elements are : 
1 1 1 
1 1 1 
1 1 1 
Difference between Sum of all rows and sum of all columns : 0

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: