Home »
VB.Net »
VB.Net Programs
VB.Net program to add two arrays
By Nidhi Last Updated : October 13, 2024
Add two arrays in VB.Net
Here, we will two arrays of integers and then read elements from the user. Then we will add the elements of both arrays and assigned them to another array. After that, we will print addition both arrays on the console screen.
Program/Source Code:
The source code to add two arrays is given below. The given program is compiled and executed successfully.
VB.Net code to add two arrays
'VB.Net program to add two arrays.
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 AddArrays(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)
AddArrays(arr1, arr2, arr3)
Console.WriteLine("Elements of Arr1: ")
PrintArray(arr1)
Console.WriteLine("Elements of Arr2: ")
PrintArray(arr2)
Console.WriteLine("Addition of Arr1 and Arr2: ")
PrintArray(arr3)
End Sub
End Module
Output
Enter array1 elements:
Element[0]: 1
Element[1]: 2
Element[2]: 3
Element[3]: 4
Element[4]: 5
Enter array2 elements:
Element[0]: 10
Element[1]: 20
Element[2]: 30
Element[3]: 40
Element[4]: 50
Elements of Arr1:
1 2 3 4 5
Elements of Arr2:
10 20 30 40 50
Addition of Arr1 and Arr2:
11 22 33 44 55
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 added the elements of both arrays using AddArray() function and then printed the result using PrintArray() on the console screen.
VB.Net Array Programs »