Home Java Beginners Sum of two Matrix
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Sum of two Matrix

Advertisement
In this section, we are going to calculate the sum of two matrix and containing its rows and columns.

Sum of  two Matrix

     

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:

class MatrixSum{
  public static void main(String[] args)  {
  int array[][]{{4,5,6},{6,8,9}};
  int array1[][]{{5,4,6},{5,6,7}};
  System.out.println("Number of Row= " + array.length);
  System.out.println("Number of Column= " + array[1].length);
  int l= array.length;
  System.out.println("Matrix 1 : ");
  for(int i = 0; i < l; i++) {
  for(int j = 0; j <= l; j++) {
  System.out.print(" "+ array[i][j]);
  }  
  System.out.println();
  }  
  int m= array1.length;
  System.out.println("Matrix 2 : ");
  for(int i = 0; i < m; i++) {
  for(int j = 0; j <= m; j++) {
  System.out.print(" "+array1[i][j]);
  }  
  System.out.println();
  }
  System.out.println("Addition of both matrix : ");
  for(int i = 0; i < m; i++) {
  for(int j = 0; j <= m; j++) {
  System.out.print(" "+(array[i][j]+array1[i][j]));
  }  
  System.out.println();
  }
  }
}

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

Download this Example

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 5, 2007

Related Tutorials

Ask Questions?    Discuss: Sum of two Matrix   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
mayur gurung
August 17, 2011
awesome

awesome explanation, it was exactly i needed for reference.......
chandika kasthuriarachchi
August 27, 2011
mettrices

its answers is very useful for me
v.vamshi krishna
September 19, 2011
to add two matrices

i want to add the matrices,the program should be like this.. at the run time only 1)i want to pass number of rows and coloumns, at the run time only 2)elements of the matrices and 3)the main requirement is when the no.of rows of first matrix = rows of second matrix columns of first matrix = columns of second matrix then only it should enter into the loop
vamsi krishna
September 19, 2011
program in java

i wnt to sho the strs in the following pattern * * * * * * * * * *
Raman Kumar ranjan
September 26, 2011
Thnx

Thank u so much
TIRUPATI
October 16, 2011
Beginner of java prog. & sql

Its a nice executed program and its very helpful to me .
madura
February 20, 2013
doubt about for loop

i cant understand how is for loop additon of both matrix
sahar
November 3, 2011
java source code

i need every program thats about java
kalaiselvan
July 10, 2012
need more

please give the program for sum of matrix give input in dynamic
pradeep sharma
July 12, 2012
java

`hey its realy very good site for finding your answers
prasad
July 17, 2012
JAVA matrix

i want 3 row and columns to added in this matrix how can i do?? exactly the below scenario to be done in java.. a=1 2 3 4 5 6 7 8 9 b=2 4 6 8 10 12 14 16 18 and need to get the out put... c=3 6 9 12 15 18 21 24 27
jin
August 13, 2012
THANKS!

THANK YOU VERY MUCH FOR THIS JAVA CODE! THIS IS REALLY A VERY BIG HELP FOR ME! AWESOME WEBSITE! KEEP IT UP! GOD BLESS!
jin
August 13, 2012
THANKS

THANK YOU VERY MUCH FOR THIS JAVA CODE! THIS IS REALLY A VERY BIG HELP FOR ME! AWESOME WEBSITE! KEEP IT UP! GOD BLESS!
hemalatha
September 12, 2012
thanx

thanx
rasmi
September 21, 2012
sum of two matrix

if i wnt 2 use thhis same prorgam for 3 rows then it generates run time error that its out of index somthing like that so how can i get solution??plz help me
DMCA.com