Java Program to calculate the sum of rows and columns in a matrix

              Matrix is a mathematical structure which consists of rows and columns. When you want to add the values of rows  and columns separately , follow the steps given below.

  • First, get the number of rows and columns from the user.
  • Initialise a new matrix with user’s rows and columns value.
  • Using a for loop, read the input for the matrix.
  • For calculating the sum of rows, use a for loop. Keep the row, navigate all column values and add the values. Do this for each row.
  • To make the sum of column values, keep the column, iterate the row values and add the value. Continue for all columns.
  • Finally, print the value.

Program:

import java.util.Scanner;

public class mRowColSum {

    public static void main(String[] args) {

        Scanner s1 = new Scanner(System.in);

       // get the matrix dimensions values

        System.out.print("Enter the number of rows: ");

        int row = s1.nextInt();

        System.out.print("Enter the number of columns: ");

        int col = s1.nextInt();

        int[][] matrix1 = new int[row][col];

        // read the matrix elements from the user

        System.out.println("Enter the matrix elements:");

        for (int i = 0; i < row; i++) {

            for (int j = 0; j < col; j++) {

                matrix1[i][j] = s1.nextInt();

            }

        }

      //Print the matrix

       System.out.println("The Matrix is :");

       for (int i = 0; i < row; i++) {

            for (int j = 0; j < col; j++) {

                System.out.print(matrix1[i][j] + " ");

            }

            System.out.println(" ");

        }

        // Calculation part and display the sum of rows value

        System.out.println("Sum of rows calculation:");

        for (int i = 0; i < row; i++) {

            int rowSum1 = 0;

            for (int j = 0; j < col; j++) {

                rowSum1 += matrix1[i][j];

            }

            System.out.println("Row " + (i + 1) + ": " + rowSum1);

        }

        // Calculation and display sum of columns value

        System.out.println("Sum of the columns:");

        for (int j = 0; j < col; j++) {

            int colSum1 = 0;

            for (int i = 0; i < row; i++) {

                colSum1 += matrix1[i][j];

            }

            System.out.println("Column " + (j + 1) + ": " + colSum1);

        }

        s1.close();

    }

}

Output:

C:\raji\blog>javac mRowColSum.java

C:\raji\blog>java mRowColSum

Enter the number of rows: 3

Enter the number of columns: 3

Enter the matrix elements:

1

1

2

3

4

5

6

7

8

The Matrix is :

1 1 2

3 4 5

6 7 8

Sum of rows calculation:

Row 1: 4

Row 2: 12

Row 3: 21

Sum of the columns:

Column 1: 10

Column 2: 12

Column 3: 15

That’s the java program to find the sum of rows and columns was written and executed successfully. Hope this code is useful to you. Keep Coding!!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.