### Addition of two matrix in java

we are going to calculate the sum of two matrix and containing its rows and columns. See below for better understanding to this:

In this program we are going to calculate the sum of two matrix. To make this program, we need to declare two dimensional array of type integer.

Now we need to make a matrix out of it. To make the matrix we will use the for loop. By making use of the for loop the rows and column will get divide. This process will be performed again for creating the second matrix. After getting both the matrix with us, we need to sum both the matrix. The both matrix will be added by using the for loop with

Now we need to make a matrix out of it. To make the matrix we will use the for loop. By making use of the for loop the rows and column will get divide. This process will be performed again for creating the second matrix. After getting both the matrix with us, we need to sum both the matrix. The both matrix will be added by using the for loop with

**C[r][c]= A[r][c]+B[r][c].**The output will be displayed by using the**print()**method.**Java version of the Addition of two matrix problem is given below:**

/** * To change this template, choose Tools | Templates * and open the template in the editor. */ package matrix; /** * * @author ACHCHUTHAN */ public class AdditionMatrix { int A[][]; int B[][]; int C[][]; int row; int coloum; public AdditionMatrix(int n, int m) { this.row = n; this.coloum = m; A = new int[n][m]; B = new int[n][m]; C = new int[n][m]; //assign random value for A,B for (int r = 0; r < n; r++) { for (int c = 0; c < m; c++) { A[r][c] = (int) Math.round(Math.random() * 98 + 10); B[r][c] = (int) Math.round(Math.random() * 12); } } } public void Addition() { for (int r = 0; r < row; r++) { for (int c = 0; c < coloum; c++) { C[r][c] = A[r][c] + B[r][c]; } } } public void print() { for (int r = 0; r < row; r++) { for (int c = 0; c < coloum; c++) { System.out.print(C[r][c] + "\t"); } System.out.println(); } } public static void main(String args[]) { System.out.println("Addition of A,B :"); AdditionMatrix S = new AdditionMatrix(4, 5); S.Addition(); S.print(); System.out.println("=======+==========+============+========="); System.out.println("Addition of A,B :"); AdditionMatrix S1 = new AdditionMatrix(4, 5); S1.Addition(); S1.print(); System.out.println("=======+==========+============+========="); } }

**Output of this program is given bellow :**

run:

Addition of A,B :

39 46 40 24 38

59 117 101 24 24

65 31 101 91 41

110 62 57 95 57

=======+============+============+=========

Addition of A,B :

14 33 84 28 80

85 52 104 74 100

48 53 52 81 63

36 107 93 118 44

=======+============+============+=========

BUILD SUCCESSFUL (total time: 1 second)