In this section, 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. Firstly it calculates the length of the both the arrays. 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 array[i][j]+array1[i][j]. The output will be displayed by using the println() method.
Here is the code of this program:
Output of program:
|C:\amar work>javac MatrixSum.java
C:\amar work>java MatrixSum
Number of Row= 2
Number of Column= 3
Matrix 1 :
4 5 6
6 8 9
Matrix 2 :
5 4 6
5 6 7
Addition of both matrix :
9 9 12
11 14 16
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: June 5, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles