Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Multpilcation of Matrix using 2D Arrays

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

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

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


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

int i = 0;
int j = 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(Console.ReadLine());

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

no_of_columns_a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Enter the number of rows for matrix B");

no_of_rows_b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Enter the number of columns for matrix B");

no_of_columns_b = Convert.ToInt32(Console.ReadLine());
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++)
{

a[i,j]= Convert.ToInt32(Console.ReadLine());

}
}
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++)
{

b[i, j] = Convert.ToInt32(Console.ReadLine());

}
}

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 (no_of_columns_a == no_of_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("");
} 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();
}
}
else
{
Console.WriteLine("The matrix multiplication cannot be performed");

}
}

You might also like