public static void Main()
Console.WriteLine("Cộng và nhân hai ma trận");
Console.WriteLine("Nhập vào số hàng ma trận A:");
m = int.Parse(Console.ReadLine());
Console.WriteLine("Nhập vào số cột ma trận A:");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Nhập vào số hàng ma trận B:");
m = int.Parse(Console.ReadLine());
Console.WriteLine("Nhập vào số cột ma trận B:");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Kết quả của phép cộng ma trận:");
Console.WriteLine("Kết quả của phép nhân ma trận:");
public static void ReadMatrix(out int [,] M, int row, int col)
for(int j=0; j < col; j++)
Console.Write("Phần tử [{0},{1}]:",i,j);
M[i,j] = int.Parse(Console.ReadLine());
public static void PrintMatrix(int [,] M)
for(int i=0; i<M.GetLength(0); i++)
for(int j=0; j<M.GetLength(1); j++)
Console.Write("{0} ,",M[i,j]);
public static void AddMatrix(int [,] a, int [,] b, out int [,] c)
c = new int[a.GetLength(0),a.GetLength(1)];
if(a.GetLength(0) != b.GetLength(0) || a.GetLength(1) != b.GetLength(1))
Console.WriteLine("Hai ma trận không cùng kích thước nên không cộng được!");
for(int i=0;i<c.GetLength(0); i++)
for(int j=0; j<c.GetLength(1);j++)
c[i,j] = a[i,j] + b[i,j];
public static void MulMatrix(int [,] a, int [,] b, out int [,] c)
c = new int[a.GetLength(0),b.GetLength(1)];
if(a.GetLength(1) != b.GetLength(0))
Console.WriteLine("Số cột của Ma trận A không bằng số hàng của Ma trận B nên không nhân được");
for(int i=0;i<a.GetLength(0); i++)
for(int j=0; j<b.GetLength(1);j++)
for ( int k=0; k<b.GetLength(0); k++ )
c[i,j] = c[i,j] + a[i,k] * b[k,j];