C# program to multiply two matrices

Here, we are going to learn how to multiply two matrices in C#?
Submitted by Nidhi, on November 02, 2020 [Last updated : March 19, 2023]

Multiplying Two Matrices

Here, we will read a matrix from the user and then calculate the multiplication of matrices.

C# code to multiply two matrices

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.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.