Q:

C# program to multiply two matrices

belongs to collection: C# Basic Programs | array programs

0

C# program to multiply two matrices

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to multiply two matrices is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to multiply two matrices.

using System;

class MatrixDemo
{
    public static void Main(string[] args)
    {
        int i = 0;
        int j = 0;

        int row     = 2;
        int col     = 2;
        
        int[,] Matrix1 = new int[row, col];
        int[,] Matrix2 = new int[row, col];
        int[,] Matrix3 = new int[row, col];

        Console.Write("Enter the elements of matrix1: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Matrix1[i, j] = int.Parse(Console.ReadLine());
            }
        }

        Console.Write("Enter the elements of matrix2: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Matrix2[i, j] = int.Parse(Console.ReadLine());
            }
        }


        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Matrix3[i, j] = 0;
                for (int k = 0; k < 2; k++)
                {
                    Matrix3[i, j] += Matrix1[i, k] * Matrix2[k, j];
                }
            }
        }


        Console.WriteLine("\nMatrix1: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Console.Write(Matrix1[i, j] + "\t");
            }
            Console.WriteLine();
        }

        Console.WriteLine("\nMatrix2: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Console.Write(Matrix2[i, j] + "\t");
            }
            Console.WriteLine();
        }

        Console.WriteLine("\nMatrix3: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Console.Write(Matrix3[i, j] + "\t");
            }
            Console.WriteLine();
        }

    }
}

Output:

Enter the elements of matrix1: 1
2
3
4
Enter the elements of matrix2: 5
6
7
8

Matrix1:
1       2
3       4

Matrix2:
5       6
7       8

Matrix3:
19      22
43      50
Press any key to continue . . .

Explanation:

In the above program, we created a class MatrixDemo that contains a Main() method. The Main() method is the entry point for the program, Here we created three 2-D arrays to represent a matrix.

Console.Write("Enter the elements of matrix1: ");
for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        Matrix1[i, j] = int.Parse(Console.ReadLine());
    }
}

Console.Write("Enter the elements of matrix2: ");
for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        Matrix2[i, j] = int.Parse(Console.ReadLine());
    }
}

In the above code, we read two matrices from the user.

for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        Matrix3[i, j] = 0;
        for (int k = 0; k < 2; k++)
        {
            Matrix3[i, j] += Matrix1[i, k] * Matrix2[k, j];
        }
    }
}

Here, we calculated the multiplication of Matrix1 and Matrix2 and assigned the result to the Matrix3. After that, we printed all matrices on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to find the sum of each row of the matr... >>
<< C# program to find the largest element in the matr...