import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
int [][] mat1 = { {3,4,5} ,
{8,5,9} };
int [][] mat2 = { {2,3,7} ,
{1,8,3} };
int [][] result ={ {0,0,0},
{0,0,0} };
System.out.println("length of matrices ="+mat1.length);
for ( int i = 0; i< mat1.length; i++)
{
System.out.print("{");
for (int j=0; j<=mat1.length; j++)
{
System.out.print(mat1[i][j]);
System.out.print(",");
}
System.out.println("}");
}
// same for mat2
System.out.println("2nd matrices");
for ( int i = 0; i< mat2.length; i++)
{
System.out.print("{");
for (int j=0; j<=mat2.length; j++)
{
System.out.print(mat2[i][j]);
System.out.print(",");
}
System.out.println("}");
}
// now both matrices sum
System.out.println("sum of both Matrices");
for (int i =0 ; i<mat1.length;i++ )
{
for (int j=0 ; j <=mat1.length; j++)
{
result[i][j] = mat1[i][j]+mat2[i][j];
System.out.print(result[i][j]+" ");
// second method
// int result2 = mat1[i][j]+mat2[i][j];
// System.out.print(result2+" ");
}
System.out.println(" ");// new line
}
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: