C# program to multiply 3*3 matrices

 


how to multiply a 3×3 matrix in c# program

c# Program to Multiply 3*3 Matrix


// 3*3 matrix multiplication value initialization by user
using System;

class MatrixMultiplication
{
    public static void Main()
    {
        int[,] matrixA = new int[3, 3];
        int[,] matrixB = new int[3, 3];
        int[,] result = new int[3, 3];

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

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

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                result[i, j] = 0;
                for (int k = 0; k < 3; k++)
                {
                    result[i, j] += matrixA[i, k] * matrixB[k, j];
                }
            }
        }

        Console.WriteLine("Resulting Matrix: ");
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.Write(result[i, j] + " ");
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

Note that matrixA and matrixB are the two matrices that are being multiplied, and result is the matrix that stores the final result of the multiplication. The outer two for loops iterate through the rows and columns of the resulting matrix, and the inner for loop iterates through the columns of matrixA and the rows of matrixB to perform the actual multiplication.

Note that the output matrix C is of the same size of A and B, which is 3x3

And each value in the resulting matrix C, can be accessed by iterating over row and column of the matrix and use the above mentioned algorithm to get the corresponding value. 

Here is an example of how to perform matrix multiplication of two 3x3 matrices in C# where the input is taken from the user:

 

using System;

This line includes the System namespace, which provides access to various classes and methods used in the program, such as the Console class and the int.Parse() method.

class MatrixMultiplication {
    public static void Main() {

This line defines a new class called MatrixMultiplication and a Main method, which is the entry point of the program.

int[,] matrixA = new int[3,3];
int[,] matrixB = new int[3,3];
int[,] result = new int[3,3];

These lines create three 2D arrays of integers, matrixA, matrixB, and result. Each array has 3 rows and 3 columns. The new keyword is used to create an instance of the array.

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

This section prompts the user to enter the elements of the first matrix, one element at a time using a nested for loop. The outer for loop iterates through the rows of the matrix, and the inner for loop iterates through the columns. The Console.WriteLine() method prints a message to the console, and the Console.ReadLine() method reads input from the user as a string. The input is then parsed to an int using the int.Parse() method, and it is stored in the corresponding element of the matrixA array.

 

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


This section is similar to the previous one and prompts the user to enter the elements of the second matrix, one element at a time and stores it in matrixB.

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                result[i, j] = 0;
                for (int k = 0; k < 3; k++) {
                    result[i, j] += matrixA[i, k] * matrixB[k, j];
                }
            }
        }

This section is the core of the matrix multiplication algorithm, which performs the multiplication of matrixA and matrixB and stores the result in result matrix. The outer two for loops iterate through the rows and columns of the resulting matrix, and the inner for loop iterates through the columns of matrixA and the rows of matrixB to perform the actual multiplication.

        Console.WriteLine("Resulting Matrix: ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                Console.Write(result[i, j] + " ");
            }
           




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.