Home »
VB.Net »
VB.Net Programs
VB.Net program to merge two integer arrays into a third array
Here, we are going to learn how to merge two integer arrays into a third array in VB.Net?
Submitted by Nidhi, on December 07, 2020 [Last updated : March 06, 2023]
Merge two integer arrays into a third array
Here, we will create two arrays of integers and then merge both arrays into a third array.
Program/Source Code:
The source code to merge two integer arrays into the third array is given below. The given program is compiled and executed successfully.
VB.Net code to merge two integer arrays into a third array
'VB.Net program to merge two integer arrays
'into the third array.
Module Module1
Sub Main()
Dim arr1 As Integer() = New Integer(5) {}
Dim arr2 As Integer() = New Integer(5) {}
Dim arr3 As Integer() = New Integer(10) {}
Console.WriteLine("Enter array1 elements: ")
For i As Integer = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr1(i) = Integer.Parse(Console.ReadLine())
Next
Console.WriteLine("Enter array2 elements: ")
For i As Integer = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr2(i) = Integer.Parse(Console.ReadLine())
Next
'Merge arr1 and arr2. and assigned to arr3.
For i As Integer = 0 To 9 Step 1
If (i <= 4) Then
arr3(i) = arr1(i)
Else
arr3(i) = arr2(i - 5)
End If
Next
Console.WriteLine("Merged array: ")
For i As Integer = 0 To 9 Step 1
Console.Write("{0} ", arr3(i))
Next
Console.WriteLine()
End Sub
End Module
Output
Enter array1 elements:
Element[0]: 12
Element[1]: 13
Element[2]: 4
Element[3]: 34
Element[4]: 35
Enter array2 elements:
Element[0]: 23
Element[1]: 56
Element[2]: 64
Element[3]: 64
Element[4]: 34
Merged array:
12 13 4 34 35 23 56 64 64 34
Press any key to continue . . .
Explanation
In the above program, we created a module Module1 that contains a function Main().
Dim arr1 As Integer() = New Integer(5) {}
Dim arr2 As Integer() = New Integer(5) {}
Dim arr3 As Integer() = New Integer(10) {}
In the Main() method we created two arrays arr1 and arr2 of five elements, and arr3 of 10 elements.
Console.WriteLine("Enter array1 elements: ")
For i As Integer = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr1(i) = Integer.Parse(Console.ReadLine())
Next
Console.WriteLine("Enter array2 elements: ")
For i As Integer = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr2(i) = Integer.Parse(Console.ReadLine())
Next
In the above code, we read the elements of arr1 and arr2 from the user.
'Merge arr1 and arr2. and assigned to arr3.
For i As Integer = 0 To 9 Step 1
If (i <= 4) Then
arr3(i) = arr1(i)
Else
arr3(i) = arr2(i - 5)
End If
Next
Console.WriteLine("Merged array: ")
For i As Integer = 0 To 9 Step 1
Console.Write("{0} ", arr3(i))
Next
In the above code, we merged arr1 and arr2 into arr3, and print the merged array on the console screen.
VB.Net Array Programs »