Addition of Two matrix
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 C[r][c]= A[r][c]+B[r][c]. The output will be displayed by using the print() method.
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.
public class addMatrix{
int A[][];
int B[][];
int C[][];
int row;
int column;
public addMatrix(int r,int c){
row=r;
column=c;
A=new int[r][c];
B=new int[r][c];
C=new int[r][c];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
A[i][j]=(int) Math.round(Math.random()*99+10);
B[i][j]=(int) Math.round(Math.random()*70+10);
}
}
}
public void addition(){
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
C[i][j]=A[i][j]+B[i][j];
}
}
}
public void display(){
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
System.out.print(C[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String args[]){
addMatrix m=new addMatrix(4,5);
m.addition();
m.display();
}
}
Output is:
C:\Users\Hari\Desktop\java>java addMatrix
134 88 115 87 161
65 188 52 166 98
86 83 43 106 93
62 104 100 49 159
Subtraction of two matrix in java
we are going to calculate the subtraction 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 subtraction 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 subtraction both the matrix. The both matrix will be subtracted 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.
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 subtraction both the matrix. The both matrix will be subtracted 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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home