VB.net Program to Insert an Item Into a One-Dimensional Array

You are currently viewing VB.net Program to Insert an Item Into a One-Dimensional Array

VB.Net program to insert an item into a one-dimensional array

'VB.Net program to insert an item into a sorted array.

Module Module1

    Sub Main()
        Dim arr As Integer() = New Integer(10) {}
        Dim n As Integer = 0
        Dim item As Integer = 0

        Console.Write("Enter size of array: ")
        n = Integer.Parse(Console.ReadLine())

        Console.WriteLine("Enter array elements: ")
        For i = 0 To n - 1 Step 1
            Console.Write("Element[{0}]: ", i)
            arr(i) = Integer.Parse(Console.ReadLine())
        Next

        Console.Write("Enter Item: ")
        item = Integer.Parse(Console.ReadLine())

        For i = 0 To 5 Step 1
            If (arr(i) >= item) Then
                For j = n To i Step -1
                    arr(j + 1) = arr(j)
                Next
                arr(i) = item
                GoTo OUT
            End If
        Next
OUT:
        Console.WriteLine("Elements of array after insertion: ")
        For i = 0 To n Step 1
            Console.WriteLine("{0}", arr(i))
        Next
    End Sub

End Module
Enter size of array: 5
Enter array elements:
Element[0]: 10
Element[1]: 20
Element[2]: 30
Element[3]: 40
Element[4]: 50
Enter Item: 35
Elements of array after insertion:
10
20
30
35
40
50
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply