Write a C#.Net program for multiplication of matrices.

Answer:

using System;

namespace MatrixMultiplication
{
    class Program
    {
        static void Main(string[] args)
        {
            // Default values for matrices
            int[,] matrix1 = {
                { 1, 2, 3 },
                { 4, 5, 6 }
            };

            int[,] matrix2 = {
                { 7, 8 },
                { 9, 10 },
                { 11, 12 }
            };

            int[,] resultMatrix = new int[2, 2];

            // Perform matrix multiplication
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    resultMatrix[i, j] = 0;
                    for (int k = 0; k < 3; k++)
                    {
                        resultMatrix[i, j] += matrix1[i, k] * matrix2[k, j];
                    }
                }
            }

            // Display the result
            Console.WriteLine("Result of matrix multiplication:");
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.Write(resultMatrix[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}
Scroll to Top