VB.Net program to subtract one array from another array

Here, we are going to learn how to subtract one array from another array in VB.Net?
Submitted by Nidhi, on December 10, 2020 [Last updated : March 06, 2023]

Subtract one array from another array

Here, we will two arrays of integers and then read elements from the user. Then we will subtract the elements of one array from another array and assign the result to the 3rd array. After that, we will print resulted array on the console screen.

Program/Source Code:

The source code to subtract one array from another array is given below. The given program is compiled and executed successfully.

VB.Net code to subtract one array from another array

'VB.Net program to subtract one array from another array.

Module Module1

    Sub ReadArray(ByVal arr() As Integer)
        For i = 0 To 4 Step 1
            Console.Write("Element[{0}]: ", i)
            arr(i) = Integer.Parse(Console.ReadLine())
        Next
    End Sub

    Sub PrintArray(ByVal arr() As Integer)
        For i = 0 To 4 Step 1
            Console.Write("{0} ", arr(i))
        Next
        Console.WriteLine()
    End Sub

    Sub SubtractArray(ByVal arr1() As Integer, ByVal arr2() As Integer, ByVal arr3() As Integer)
        For i = 0 To 4 Step 1
            arr3(i) = arr1(i) - arr2(i)
        Next
    End Sub

    Sub Main()
        Dim arr1 As Integer() = New Integer(5) {}
        Dim arr2 As Integer() = New Integer(5) {}
        Dim arr3 As Integer() = New Integer(5) {}

        Console.WriteLine("Enter array1 elements: ")
        ReadArray(arr1)

        Console.WriteLine("Enter array2 elements: ")
        ReadArray(arr2)

        SubtractArray(arr1, arr2, arr3)

        Console.WriteLine("Elements of Arr1: ")
        PrintArray(arr1)

        Console.WriteLine("Elements of Arr2: ")
        PrintArray(arr2)

        Console.WriteLine("Resulted array: ")
        PrintArray(arr3)
    End Sub
End Module

Output

Enter array1 elements:
Element[0]: 20
Element[1]: 21
Element[2]: 22
Element[3]: 23
Element[4]: 24
Enter array2 elements:
Element[0]: 10
Element[1]: 11
Element[2]: 12
Element[3]: 13
Element[4]: 14
Elements of Arr1:
20 21 22 23 24
Elements of Arr2:
10 11 12 13 14
Resulted array:
10 10 10 10 10
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 arrays. Here, we will read the elements of arr1 and arr2 using ReadArry() function. After that, we subtracted the elements of arr2 from arr1 using SubtractArray() function and then printed the result using PrintArray() on the console screen.

VB.Net Array Programs »





Comments and Discussions!

Load comments ↻





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