Matrix Multiplication - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Matrix Multiplication

Posts: 8Member
in C#
Hey!
I am just a beginner. I am trying to multiply 2 matrices but the code is not working well.Its not multiplying 1st row with 1st column,1st row with 2nd column, 2nd row with 1st column and 2nd row with 2nd column.
Its just multiplying 1st element of 1st matrix with 1st element of 2nd matrix, etc.
Heres my code Help me with the matrix multiplication.
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[,] matrix1 = new int[2, 2];
int[,] matrix2 = new int[2, 2];
int[,] result = new int[2, 2];

//Taking Input 1st Matrix
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine("Enter 1st Matrix: ");
}
} Console.WriteLine();

//Taking Input 2nd Matrix
for (int k = 0; k < 2; k++)
{
for (int l = 0; l < 2; l++)
{
Console.WriteLine("Enter 2st Matrix: ");
}
} Console.WriteLine();

//Printing 1st Matrix
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();

//Printing 2nd Matrix
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++)
{
Console.Write(matrix1[i, j] + matrix2[i, j] + " ");
} Console.WriteLine();
} Console.WriteLine();

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

//Multiplying Matrices
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();