×

VB.Net Programs

VB.Net program to multiply two matrices

By Nidhi Last Updated : November 15, 2024

Multiply two matrices

Here, we will read two matrices from the user then multiply both matrices and assigned them to another matrix. After that, we will all matrices on the console screen.

Program/Source Code:

The source code to multiply two matrices is given below. The given program is compiled and executed successfully.

VB.Net code to multiply two matrices

'VB.Net program to multiply two matrices.

Module Module1

    Sub Main()
        Dim matrix1(,) As Integer = New Integer(2, 2) {}
        Dim matrix2(,) As Integer = New Integer(2, 2) {}
        Dim matrix3(,) As Integer = New Integer(2, 2) {}
        Dim sum As Integer = 0

        Console.WriteLine("Enter Matrix1: ")
        For i = 0 To 1 Step 1
            For j = 0 To 1 Step 1
                Console.Write("Enter element[{0}][{1}]: ", i, j)
                matrix1(i, j) = Integer.Parse(Console.ReadLine())
            Next
        Next

        Console.WriteLine("Enter Matrix2: ")
        For i = 0 To 1 Step 1
            For j = 0 To 1 Step 1
                Console.Write("Enter element[{0}][{1}]: ", i, j)
                matrix2(i, j) = Integer.Parse(Console.ReadLine())
            Next
        Next

        'Multiplication of two matrices
        For i = 0 To 1 Step 1
            For j = 0 To 1 Step 1
                sum = 0
                For k = 0 To 1 Step 1
                    sum = sum + (matrix1(i, k) * matrix2(k, j))
                Next
                matrix3(i, j) = sum
            Next
        Next
        Console.WriteLine("Matrix1: ")
        For i = 0 To 1 Step 1
            For j = 0 To 1 Step 1
                Console.Write("{0} ", matrix1(i, j))
            Next
            Console.WriteLine()
        Next

        Console.WriteLine("Matrix2: ")
        For i = 0 To 1 Step 1
            For j = 0 To 1 Step 1
                Console.Write("{0} ", matrix2(i, j))
            Next
            Console.WriteLine()
        Next
        Console.WriteLine("Multiplication of Matrix1 and Matrix2: ")
        For i = 0 To 1 Step 1
            For j = 0 To 1 Step 1
                Console.Write("{0} ", matrix3(i, j))
            Next
            Console.WriteLine()
        Next
    End Sub
    
End Module

Output

Enter Matrix1:
Enter element[0][0]: 10
Enter element[0][1]: 20
Enter element[1][0]: 20
Enter element[1][1]: 10
Enter Matrix2:
Enter element[0][0]: 10
Enter element[0][1]: 20
Enter element[1][0]: 20
Enter element[1][1]: 10
Matrix1:
10 20
20 10
Matrix2:
10 20
20 10
Multiplication of Matrix1 and Matrix2:
500 400
400 500
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains a function Main(). In the Main(), we created three matrices of 2x2 using a two-dimensional array.

Console.WriteLine("Enter Matrix1: ")
For i = 0 To 1 Step 1
    For j = 0 To 1 Step 1
        Console.Write("Enter element[{0}][{1}]: ", i, j)
        matrix1(i, j) = Integer.Parse(Console.ReadLine())
    Next
Next

Console.WriteLine("Enter Matrix2: ")
For i = 0 To 1 Step 1
    For j = 0 To 1 Step 1
        Console.Write("Enter element[{0}][{1}]: ", i, j)
        matrix2(i, j) = Integer.Parse(Console.ReadLine())
    Next
Next

In the above code, we read elements for both matrices from the user.

'Multiplication of two matrices
    For i = 0 To 1 Step 1
        For j = 0 To 1 Step 1
            sum = 0
            For k = 0 To 1 Step 1
                sum = sum + (matrix1(i, k) * matrix2(k, j))
            Next
            matrix3(i, j) = sum
        Next
    Next

Here, in the above code, we multiply matrix1 and matrix2 and assigned the multiplication of both matrices into matrix3. After that, we printed the all matrices on the console screen.

VB.Net Array Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.