C# Program to Multiply Two Matrices

This is a C# Program to perform matrix multiplication.

Problem Description

This C# Program Performs Matrix Multiplication.

Problem Solution

Here the matrix multiplication is performed if the number of columns of the first matrix is equal to the number of rows of the second matrix. For loop is used to display the values in a matrix format.

Program/Source Code

Here is source code of the C# Program to Perform Matrix Multiplication. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Perform Matrix Multiplication
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace matrix_multiplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j,m,n;
            Console.WriteLine("Enter the Number of Rows and Columns : ");
            m = Convert.ToInt32(Console.ReadLine());
            n = Convert.ToInt32(Console.ReadLine());
            int[,] a = new int[m, n];
            Console.WriteLine("Enter the First Matrix");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    a[i, j] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("First matrix is:");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(a[i, j] + "\t");
                }
                Console.WriteLine();
            }
            int[,] b = new int[m, n];
            Console.WriteLine("Enter the Second Matrix");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    b[i, j] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("Second Matrix is :");
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    Console.Write(b[i, j] + "\t");
                }
                Console.WriteLine();
            }
            Console.WriteLine("Matrix Multiplication is :");
            int[,] c = new int[m, n];
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    c[i, j] = 0;
                    for (int k = 0; k < 2; k++)
                    {
                        c[i, j] += a[i, k] * b[k, j];
                    }
                }
            }
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(c[i, j] + "\t");
                }
                Console.WriteLine();
            }
 
            Console.ReadKey();
        }
    }
}
Program Explanation

In this C# program we are reading the number of rows and columns for two matrices using the variable ‘m’ and ‘n’ respectively. Using for loop we are entering the coefficient values for first matrix and second matrix.

advertisement
advertisement

The matrix multiplication is performed if the number of columns of the first matrix is equal to the number of rows of the second matrix. For loop is used to print the values in a matrix format.

Runtime Test Cases
 
Enter the First Matrix
8
7
6
10
First Matrix is :
8       7
6       10
Enter the Second Matrix
4
3
2
1
Second Matrix is :
4       3
2       1
Matrix multiplication is :
46      31
44      28

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.