using
System;
class
GFG
{
const
int
MAX = 100;
static
void
printMatrix(
int
[,] M,
int
rowSize,
int
colSize)
{
for
(
int
i = 0; i < rowSize; i++)
{
for
(
int
j = 0; j < colSize; j++)
Console.Write(M[i, j] +
" "
);
Console.WriteLine();
}
}
static
void
multiplyMatrix(
int
row1,
int
col1,
int
[,] A,
int
row2,
int
col2,
int
[,] B)
{
int
i, j, k;
int
[,] C =
new
int
[MAX, MAX];
if
(row2 != col1)
{
Console.WriteLine(
"Not Possible"
);
return
;
}
for
(i = 0; i < row1; i++)
{
for
(j = 0; j < col2; j++)
{
C[i, j] = 0;
for
(k = 0; k < row2; k++)
C[i, j] += A[i, k] * B[k, j];
}
}
Console.WriteLine(
"\nResultant Matrix: "
);
printMatrix(C, row1, col2);
}
static
void
Main(
string
[] args)
{
int
row1, col1, row2, col2, i, j;
int
[,] A =
new
int
[MAX, MAX];
int
[,] B =
new
int
[MAX, MAX];
Console.Write(
"Enter the number of rows of First Matrix: "
);
row1 = Convert.ToInt32(Console.ReadLine());
Console.Write(
"Enter the number of columns of First Matrix: "
);
col1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(
"Enter the elements of First Matrix: "
);
for
(i = 0; i < row1; i++)
{
for
(j = 0; j < col1; j++)
{
Console.Write(
"["
+ i +
"]["
+ j +
"] = "
);
A[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.Write(
"Enter the number of rows of Second Matrix: "
);
row2 = Convert.ToInt32(Console.ReadLine());
Console.Write(
"Enter the number of columns of Second Matrix: "
);
col2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(
"Enter the elements of Second Matrix: "
);
for
(i = 0; i < row2; i++)
{
for
(j = 0; j < col2; j++)
{
Console.Write(
"["
+ i +
"]["
+ j +
"] = "
);
B[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine(
"\nFirst Matrix: "
);
printMatrix(A, row1, col1);
Console.WriteLine(
"\nSecond Matrix: "
);
printMatrix(B, row2, col2);
multiplyMatrix(row1, col1, A, row2, col2, B);
Console.ReadLine();
}
}