# Matrix multiplication in C#

Kindly remember the multiplication logic for matrices.
The no of columns of first matrix must be equal to no of rows of second matrix.
However, it is handled in the program code.

Please press 'enter' button after entering each value.

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try

{
string rows_a;

string columns_a;

string rows_b;

string columns_b;

string m;

string n;

int[,] a = new int[4, 4];

int[,] b = new int[4, 4];

int[,] c = new int[4, 4];

int i = 0;

int j = 0;

int k = 0;

int l = 0;

c[i, j] = 0;

int no_of_rows_a = 0;

int no_of_columns_a = 0;

int no_of_rows_b = 0;

int no_of_columns_b = 0;

Console.WriteLine(" Enter the number of rows for matrix A");

no_of_rows_a = Convert.ToInt32(rows_a);

Console.WriteLine(" Enter the number of columns for matrix A");

no_of_columns_a = Convert.ToInt32(columns_a);

Console.WriteLine(" Enter the number of rows for matrix B");

no_of_rows_b = Convert.ToInt32(rows_b);

Console.WriteLine(" Enter the number of columns for matrix B");

no_of_columns_b = Convert.ToInt32(columns_b);

Console.WriteLine(" The values of A matrix is :");

for (i = 0; i < no_of_rows_a; i++)

{

for (j = 0; j < no_of_columns_a; j++)

{

k = Convert.ToInt32(m);

a[i, j] = k;

}

}

Console.WriteLine(" The values of B matrix is :");

for (i = 0; i < no_of_rows_b; i++)

{

for (j = 0; j < no_of_columns_b; j++)

{

l = Convert.ToInt32(n);

b[i, j] = l;

}

}

Console.WriteLine(" The matrix format of A is ");

for (i = 0; i < no_of_rows_a; i++)

{

for (j = 0; j < no_of_columns_a; j++)

{

Console.Write(" {0}", a[i, j]);

}

Console.WriteLine("");

}

Console.WriteLine(" The matrix format of B is ");

for (i = 0; i < no_of_rows_b; i++)

{

for (j = 0; j < no_of_columns_b; j++)

{

Console.Write(" {0}",b[i,j]);

}

Console.WriteLine("");

}

if (columns_a == rows_b)

{

for (i = 0; i < no_of_rows_a; i++)

{

for (j = 0; j < no_of_columns_b; j++)

{

c[i, j] = (a[i, 0] * b[0, j]) + (a[i, 1] * b[1, j]);

Console.Write("  ");

}

Console.WriteLine("");

}

}

else

{

Console.WriteLine("The matrix multiplication cannot be performed: Logic violation");

}

Console.WriteLine(" The resultant matrix is");

for (i = 0; i < no_of_rows_a; i++)

{

for (j = 0; j < no_of_columns_b; j++)

{

Console.Write(" {0}", c[i, j]);

}

Console.WriteLine();

}

}

catch (Exception e)
{

Console.WriteLine(e.Message);

}
}
}
}

```