Sum of row in 2d array java: In the previous article, we have discussed Java Program to Check Whether a Given Matrix is an Identity Matrix
In this article we are going to see how we can write a program to find the sum of each row and column of a matrix in JAVA language.
Java Program to Find the Sum of Each Row and Each Column 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
wherei=0
andj=0
,A01=aij
wherei=0
andj=1
and like this. - Here we have started
row
value from 0 andcolumn
value from 0.
Let’s see different ways to check sum of each row and each column of a matrix.
Method-1: Java Program to Find the Sum of Each Row and Each Column of a Matrix By Static Initialization of Array Elements
Approach:
- Initialize and declare three arrays of, one with size 3×3 which is for matrix, rest empty with size 3 each which is for holding each row and each column sum.
- Use two for loops to iterate the rows and columns .
- Inside the for loops calculate the row and column sum together and store them in the blank arrays.
- 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[][] = {{19,25,32},{40,54,62},{70,20,60}}; int rowSum[] = new int[3],colSum[] = new int[3]; int row, col ; System.out.print("The matrix elements are : "); printMatrix(arr); // Loops to calculate the row and column sums for(row=0;row<3;row++) { for(col=0;col<3;col++) { rowSum[row] += arr[row][col]; colSum[row] += arr[col][row]; } } // Loop to print the rows and column sums for(row=0;row<3;row++) { System.out.print("\nSum of "+(row+1)+" row = "+rowSum[row]); System.out.print("\nSum of "+(row+1)+" column = "+colSum[row]); } } // 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]+" "); } } } }
Output: The matrix elements are : 19 25 32 40 54 62 70 20 60 Sum of 1 row = 76 Sum of 1 column = 129 Sum of 2 row = 156 Sum of 2 column = 99 Sum of 3 row = 150 Sum of 3 column = 154
Method-2: Java Program to Find the Sum of Each Row and Each Column of a Matrix By Dynamic Initialization of Array Elements
Approach:
- Declare three arrays of, one with size 3×3 which is for matrix, rest empty with size 3 each which is for holding each row and each column sum.
- Ask the user for input and store them in the one array using two for loops
- Use two for loops to iterate the rows and columns .
- Inside the for loops calculate the row and column sum together and store them in the blank arrays.
- Print the result using a for loop.
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]; //Arrays declared toholfd row andcolumn sum int rowSum[] = new int[3],colSum[] = new int[3]; int row, col ; // Taking matrix1 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); // Loops to calculate the row and column sums for(row=0;row<3;row++) { for(col=0;col<3;col++) { rowSum[row] += arr[row][col]; colSum[row] += arr[col][row]; } } // Loop to print the rows and column sums for(row=0;row<3;row++) { System.out.print("\nSum of "+(row+1)+" row = "+rowSum[row]); System.out.print("\nSum of "+(row+1)+" column = "+colSum[row]); } } // 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]+" "); } } System.out.print("\n"); } }
Output: Enter matrix elements : The matrix elements are : 11 21 31 10 12 11 13 14 15 Sum of 1 row = 63 Sum of 1 column = 34 Sum of 2 row = 33 Sum of 2 column = 47 Sum of 3 row = 42 Sum of 3 column = 57
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: