Basic Programming C# Examples

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below:

using System;

namespace MatrixMultiplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number of rows/columns in the matrices: ");
            int n = int.Parse(Console.ReadLine());

            int[,] firstMatrix = new int[n, n];
            int[,] secondMatrix = new int[n, n];
            int[,] resultMatrix = new int[n, n];

            Console.WriteLine("Enter the elements of the first matrix: ");
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    firstMatrix[i, j] = int.Parse(Console.ReadLine());
                }
            }

            Console.WriteLine("Enter the elements of the second matrix: ");
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    secondMatrix[i, j] = int.Parse(Console.ReadLine());
                }
            }

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    for (int k = 0; k < n; k++)
                    {
                        resultMatrix[i, j] += firstMatrix[i, k] * secondMatrix[k, j];
                    }
                }
            }

            Console.WriteLine("Okay, the result matrix after multiplication is: ");
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write(resultMatrix[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

Output:


Hello Codeauri Family, enter the number of rows/columns in the matrices:
2
Enter the elements of the first matrix:
4
7
6
9
Enter the elements of the second matrix:
3
5
1
2
Okay, the result matrix after multiplication is:
19 34
27 48

Pro-Tips💡

Here are the steps by steps execution of above program:

  1. The first line using System; includes the System namespace, which contains various classes and methods.
  2. The code defines a namespace MatrixMultiplication to group together related classes.
  3. Within the MatrixMultiplication namespace, the code defines a class Program with a Main method that is the entry point of the program.
  4. The program prompts the user to enter the number of rows/columns in the matrices using Console.WriteLine method.
  5. The program uses the entered value to declare two two-dimensional arrays firstMatrix and secondMatrix and another two-dimensional array resultMatrix to store the result of the matrix multiplication.
  6. The program then prompts the user to enter the elements of the first matrix and stores them in the firstMatrix array.
  7. The program then prompts the user to enter the elements of the second matrix and stores them in the secondMatrix array.
  8. The program then uses a nested for loop to multiply the matrices and store the result in the resultMatrix array.
  9. The program finally displays the result matrix using another nested for loop.
  10. The program ends with the Console.WriteLine method, which writes a blank line to the console.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X