6 C. Write a java program for multiplying two matrices and print the result for the same.
Program :
import java.util.Scanner;
public class MatAdd
{
public static void main(String args[])
{
int i, j;
int mat1[][] = new int[2][2];
int mat2[][] = new int[2][2];
int mat3[][] = new int[2][2];
Scanner sc = new Scanner(System.in);
System.out.print("Enter Matrix 1 Elements : ");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
mat1[i][j] = sc.nextInt();
}
}
System.out.print("Enter Matrix 2 Elements : ");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
mat2[i][j] = sc.nextInt();
}
}
System.out.print("Adding both Matrix to form the Third Matrix...\n");
System.out.print("The Two Matrix Added Successfully..!!\n");
System.out.print("The New Matrix will be :\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
mat3[i][j] = mat1[i][j] * mat2[i][j];
System.out.print(mat3[i][j]+ " ");
}
System.out.println("");
}
}
}
Comments
Post a Comment