C# program to multiply two matrices

Matrix multiplication is a mathematical operation that combines two matrices to produce a third matrix. This operation is only possible when the number of columns in the first matrix equals the number of rows in the second matrix. If matrix A has dimensions m×n and matrix B has dimensions n×p, the resulting matrix C will have dimensions m×p.

Matrix Multiplication Rule Matrix A m × n 2 × 3 × Matrix B n × p 3 × 3 = Matrix C m × p 2 × 3 Condition: columns of A = rows of B n (columns of A) must equal n (rows of B) Result has dimensions: rows of A × columns of B

Matrix Multiplication Algorithm

The algorithm follows these steps −

  • Check if multiplication is possible (columns of first matrix = rows of second matrix)

  • Create a result matrix with dimensions (rows of first matrix × columns of second matrix)

  • For each cell in the result matrix, calculate the dot product of the corresponding row and column

Example

The following program demonstrates matrix multiplication in C# −

using System;

namespace MatrixMultiplicationDemo {
    class Example {
        static void Main(string[] args) {
            int m = 2, n = 3, p = 3, q = 3, i, j;
            int[,] a = {{1, 4, 2}, {2, 5, 1}};
            int[,] b = {{3, 4, 2}, {3, 5, 7}, {1, 2, 1}};
            
            Console.WriteLine("Matrix a:");
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    Console.Write(a[i, j] + " ");
                }
                Console.WriteLine();
            }
            
            Console.WriteLine("Matrix b:");
            for (i = 0; i < p; i++) {
                for (j = 0; j < q; j++) {
                    Console.Write(b[i, j] + " ");
                }
                Console.WriteLine();
            }
            
            if (n != p) {
                Console.WriteLine("Matrix multiplication not possible");
            } else {
                int[,] c = new int[m, q];
                for (i = 0; i < m; i++) {
                    for (j = 0; j < q; j++) {
                        c[i, j] = 0;
                        for (int k = 0; k < n; k++) {
                            c[i, j] += a[i, k] * b[k, j];
                        }
                    }
                }
                
                Console.WriteLine("The product of the two matrices is:");
                for (i = 0; i < m; i++) {
                    for (j = 0; j < q; j++) {
                        Console.Write(c[i, j] + "\t");
                    }
                    Console.WriteLine();
                }
            }
        }
    }
}

The output of the above code is −

Matrix a:
1 4 2 
2 5 1 
Matrix b:
3 4 2 
3 5 7 
1 2 1 
The product of the two matrices is:
17	26	32
23	35	40

How It Works

First, the program displays both input matrices. The condition n != p checks if matrix multiplication is possible −

if (n != p) {
    Console.WriteLine("Matrix multiplication not possible");
}

The multiplication uses three nested loops. The innermost loop calculates the dot product for each element in the result matrix −

for (i = 0; i < m; i++) {
    for (j = 0; j < q; j++) {
        c[i, j] = 0;
        for (int k = 0; k < n; k++) {
            c[i, j] += a[i, k] * b[k, j];
        }
    }
}

For element c[0,0], the calculation is: (1×3) + (4×3) + (2×1) = 3 + 12 + 2 = 17.

Using Methods for Better Organization

Here's an improved version with separate methods for better code organization −

using System;

class MatrixOperations {
    static void DisplayMatrix(int[,] matrix, int rows, int cols, string name) {
        Console.WriteLine($"Matrix {name}:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                Console.Write(matrix[i, j] + "\t");
            }
            Console.WriteLine();
        }
    }
    
    static int[,] MultiplyMatrices(int[,] a, int[,] b, int m, int n, int p) {
        int[,] result = new int[m, p];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < p; j++) {
                result[i, j] = 0;
                for (int k = 0; k < n; k++) {
                    result[i, j] += a[i, k] * b[k, j];
                }
            }
        }
        return result;
    }
    
    static void Main(string[] args) {
        int[,] matrix1 = {{2, 3}, {1, 4}, {5, 6}};
        int[,] matrix2 = {{1, 2, 3}, {4, 5, 6}};
        
        int m = 3, n = 2, p = 3;
        
        DisplayMatrix(matrix1, m, n, "A");
        DisplayMatrix(matrix2, n, p, "B");
        
        if (n == matrix2.GetLength(0)) {
            int[,] result = MultiplyMatrices(matrix1, matrix2, m, n, p);
            DisplayMatrix(result, m, p, "Result (A × B)");
        } else {
            Console.WriteLine("Matrix multiplication not possible");
        }
    }
}

The output of the above code is −

Matrix A:
2	3	
1	4	
5	6	
Matrix B:
1	2	3	
4	5	6	
Matrix Result (A × B):
14	19	24	
17	22	27	
29	40	51	

Conclusion

Matrix multiplication in C# requires checking dimensional compatibility and using nested loops to calculate dot products. The operation is fundamental in mathematical computing and can be implemented efficiently using multi-dimensional arrays and proper loop structures.

Updated on: 2026-03-17T07:04:35+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements