java program to add two matrices
Answers
Answered by
1
Hola
We can add two matrices in java using binary + operator. A matrix is also known as array of arrays. We can add, subtract and multiply matrices.
To subtract two matrices, use - operator. Following is the program to add two matrices of 3 rows and 3 columns.
public class MatrixAdditionExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
//creating another matrix to store the sum of two matrices
int c[][]=new int[3][3];
//3 rows and 3 columns
//adding and printing addition of 2 matrices for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
//use - for subtraction System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}}
hope u got it
We can add two matrices in java using binary + operator. A matrix is also known as array of arrays. We can add, subtract and multiply matrices.
To subtract two matrices, use - operator. Following is the program to add two matrices of 3 rows and 3 columns.
public class MatrixAdditionExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
//creating another matrix to store the sum of two matrices
int c[][]=new int[3][3];
//3 rows and 3 columns
//adding and printing addition of 2 matrices for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
//use - for subtraction System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}}
hope u got it
Answered by
1
import
java.util.Scanner;
class AddTwoMatrix
{
public static void main(String args[])
{ int m, n, c, d; Scanner in = new
Scanner(System.in);
System.out.printl
("Enter the number of rows and columns of matrix"); m = in.nextInt(); n =
in.nextInt(); int first[][] = new int[m][n];
int second[][] = new int[m][n]; int sum[][] = new int[m][n];
System.out.println("Enter the elements of first matrix"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) first[c][d] = in.nextInt();
System.out.println("Enter the elements of second matrix");
for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ )
second[c][d] = in.nextInt(); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++
) sum[c][d] = first[c][d] + second[c][d]; //replace '+'
with '-' to subtract
matrices
System.out.println("Sum of entered matrices:-"); for ( c = 0 ; c < m ; c++
) { for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");
System.out.println(); } } }
java.util.Scanner;
class AddTwoMatrix
{
public static void main(String args[])
{ int m, n, c, d; Scanner in = new
Scanner(System.in);
System.out.printl
("Enter the number of rows and columns of matrix"); m = in.nextInt(); n =
in.nextInt(); int first[][] = new int[m][n];
int second[][] = new int[m][n]; int sum[][] = new int[m][n];
System.out.println("Enter the elements of first matrix"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) first[c][d] = in.nextInt();
System.out.println("Enter the elements of second matrix");
for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ )
second[c][d] = in.nextInt(); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++
) sum[c][d] = first[c][d] + second[c][d]; //replace '+'
with '-' to subtract
matrices
System.out.println("Sum of entered matrices:-"); for ( c = 0 ; c < m ; c++
) { for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");
System.out.println(); } } }
Similar questions