java program to add two matrices
Answers
Answered by
1
Heya,
here's ur answer
importjava.util.Scanner;
class AddingTwoMatrices
{publicstaticvoid main(String args[]){int m, n, c, d;
Scanner in =new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int first[][]=newint[m][n];int second[][]=newint[m][n];int sum[][]=newint[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();}}}
Hope u got it
Have a nice day ahead
here's ur answer
importjava.util.Scanner;
class AddingTwoMatrices
{publicstaticvoid main(String args[]){int m, n, c, d;
Scanner in =new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int first[][]=newint[m][n];int second[][]=newint[m][n];int sum[][]=newint[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();}}}
Hope u got it
Have a nice day ahead
Answered by
1
Hola
Following is the program which can be used to add two matrices
...
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
Following is the program which can be used to add two matrices
...
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
Similar questions