Java Program to Find Sum of All Rows and Sum of All Columns of a Matrix

In the previous article, we have seen Java Program to Find Product of Sum of n-th row and n-th column

In this article we are going to see how we can write a program to find the sum of all rows and all columns of a matrix in JAVA language.

Java Program to Find Sum of All Rows and Sum of All Columns of a Matrix

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. 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.

Let’s see different ways to find the sum of all rows and all columns of a matrix.

Method-1: Java Program to Find Sum of All Rows and Sum of All Columns of a Matrix 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.
  • 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];
            }   
        }
       
       //Printing the sum of all rows and all columns
        System.out.print("\nSum of all rows : "+rowSum);
        System.out.print("\nSum of all columns : "+colSum);
    }
    
    //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 
Sum of all rows : 440
Sum of all columns : 440

Method-2: Java Program to Find Sum of All Rows and Sum of All Columns of a Matrix 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.
  • 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];
            }   
        }
       
       //Printing the sum of all rows and all columns
        System.out.print("\nSum of all rows : "+rowSum);
        System.out.print("\nSum of all columns : "+colSum);
    }
    
    //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 
Sum of all rows : 9
Sum of all columns : 9

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: