A- Rows in first Matrix
B- Columns for first Matrix and Rows for Second Matrix
C- Columns for Second Matrix
package com.talent;
import java.util.Scanner;
public class MatrixMulti {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the rows for first matrix");
int A=sc.nextInt();
System.out.println("enter the no. for columns in first matirx / rows in second matrix ");
int B=sc.nextInt();
System.out.println("enter the columns in second matrix");
int C=sc.nextInt();
//now define all three matrix
int [][]matrix1= new int[A][B];
int [][]matrix2= new int[B][C];
int [][]product= new int[A][C];
System.out.println("enter the data for first elements");
for(int i=0; i<A; i++)
{
for(int j=0; j<B; j++)
{
matrix1[i][j]=sc.nextInt();
}
}
System.out.println("enter the data of second");
for(int i=0; i<B; i++)
{
for(int j=0; j<C; j++)
{
matrix2[i][j]=sc.nextInt();
}
}
System.out.println("first matirx is : ");
for(int i=0; i<A; i++)
{
for(int j=0;j<B;j++)
{
System.out.print(matrix1[i][j]+"\t");
}
System.out.println();
}
System.out.println("second matirx is: ");
for(int i=0; i<B; i++)
{
for(int j=0; j<C; j++)
{
System.out.print(matrix2[i][j]+"\t");
}
System.out.println();
}
System.out.println("PRODUCT IS : ");
for(int i=0; i<A;i++)
{
for(int j=0; j<C; j++)
{
for(int k=0; k<B;k++)
{
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
for(int i=0; i<A; i++)
{
for(int j=0; j<C; j++)
{
System.out.print(product[i][j]+"\t");
}
System.out.println();
}
}
}
OUTPUT->enter the rows for first matrix
2
enter the no. for columns in first matirx / rows in second matrix
3
enter the columns in second matrix
2
enter the data for first elements
3
4
5
6
7
8
enter the data of second
3
4
5
6
7
8
first matirx is :
3 4 5
6 7 8
second matirx is:
3 4
5 6
7 8
PRODUCT IS :
64 76
109 130