Search This Blog

Loading...

Oct 7, 2010

Matrix Multiplication in C#


Sponsored Links:
namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
// Matrix Manipulation
int[,] matrix1 = new int[2, 2];
int[,] matrix2 = new int[2, 2];
int[,] result = new int[2, 2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine("Enter 1st Matrix: ");
matrix1[i, j] = Convert.ToInt32(Console.ReadLine());
}
}      Console.WriteLine();

for (int k = 0; k < 2; k++)
{
for (int l = 0; l < 2; l++)
{
Console.WriteLine("Enter 2nd Matrix: ");
matrix2[k, l] = Convert.ToInt32(Console.ReadLine());
}
}      Console.WriteLine();

Console.WriteLine("Matrix 1: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(matrix1[i, j] + " ");
}   Console.WriteLine();
}      Console.WriteLine();

Console.WriteLine("Matrix 2: ");
for (int k = 0; k < 2; k++)
{
for (int l = 0; l < 2; l++)
{
Console.Write(matrix2[k, l] + " ");
}   Console.WriteLine();
}      Console.WriteLine();

Console.WriteLine("Matrix 1 * Matrix 2: ");
for (int i = 0; i < 2; i++)  
{  
for (int j = 0; j < 2; j++)  
{  
result[i, j] += matrix1[i, j] * matrix2[i, j];
Console.Write(result[i, j] + " ");
}  Console.WriteLine();
}  Console.ReadLine();
Console.ReadLine();
}
}

}

Output

image

Follow by Email