public static void Main()
int[, ] A = new int[, ]{{1, 2, 3}, {4, 5, 6}};
System.Console.WriteLine("A = ");
System.Console.WriteLine();
int[, ] B = new int[, ]{{7, 8}, {8, 9}, {9, 0}};
System.Console.WriteLine("B = ");
System.Console.WriteLine();
int[, ] C = Multiply(A, B);
System.Console.WriteLine("A x B = ");
public static int[, ] Multiply(int[, ] A, int[, ] B)
if (A.GetLength(1) != B.GetLength(0))
throw new NotSupportedException("Columns of A and Rows of B must be equal.");
int[, ] result = new int[A.GetLength(0), B.GetLength(1)];
Console.WriteLine("{0,4} | {1,4} | {2,4} | {3,6} | {4,6} | {5,6}", "i", "j", "k", "A[i,k]", "B[k,j]", "C[i,j]");
Console.WriteLine(new string ('=', 3 * 4 + 3 * 6 + 5 * 3));
for (int i = 0; i < A.GetLength(0); i++)
Console.WriteLine("{0,4} | {1,4} | {2,4} | {3,6} | {4,6} | {5,6}", i, "", "", "", "", "");
for (int j = 0; j < B.GetLength(1); j++)
Console.WriteLine("{0,4} | {1,4} | {2,4} | {3,6} | {4,6} | {5,6}", "", j, "", "", "", "");
Console.WriteLine("{0,4} | {1,4} | {2,4} | {3,6} | {4,6} | {5,6}", "", "", "", "", "", result[i, j]);
for (int k = 0; k < A.GetLength(1); k++)
Console.WriteLine("{0,4} | {1,4} | {2,4} | {3,6} | {4,6} | {5,6}", "", "", k, A[i, k], B[k, j], "");
result[i, j] += A[i, k] * B[k, j];
Console.WriteLine("{0,4} | {1,4} | {2,4} | {3,6} | {4,6} | {5,6}", "", "", "", "", "", result[i, j]);
public static void Print2DMatrix(int[, ] M)
for (int i = 0; i <= M.GetUpperBound(0); i++)
for (int j = 0; j <= M.GetUpperBound(1); j++)
Console.Write("{0,6}", M[i, j]);