VB.Net program to search an item in array using linear search

Here, we are going to learn how to search an item in array using linear search in VB.Net?
Submitted by Nidhi, on December 08, 2020 [Last updated : March 06, 2023]

Search an item in array using linear search

Here, we will create an array of integers and then read elements from the user, and search item in the array, if item found then print the location of item in array.

Program/Source Code:

The source code to search an item in array using linear search is given below. The given program is compiled and executed successfully.

VB.Net code to search an item in array using linear search

'VB.Net program to search an item in array 
'using linear search.

Module Module1

    Sub Main()
        Dim arr As Integer() = New Integer(5) {}
        Dim item As Integer = 0
        Dim flag As Integer = -1
        Dim i As Integer = 0

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

        Console.WriteLine("Enter item for searching: ")
        item = Integer.Parse(Console.ReadLine())

        For i = 0 To 4 Step 1
            If item = arr(i) Then
                flag = i
                GoTo out
            End If
        Next
out:
        If flag <> -1 Then
            Console.WriteLine("Item found at index {0} in array", flag)
        Else
            Console.WriteLine("Item is not found")
        End If
    End Sub

End Module

Output

Enter array elements:
Element[0]: 10
Element[1]: 20
Element[2]: 30
Element[3]: 40
Element[4]: 50
Enter item for searching:
30
Item found at index 2 in array
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 an array arr of five elements and two variables item and flag that are initialized with 0.

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

In the above code, we read the elements of the array from the user.

Console.WriteLine("Enter item for searching: ")
item = Integer.Parse(Console.ReadLine())

For i = 0 To 4 Step 1
    If item = arr(i) Then
        flag = i
        GoTo out
    End If
Next
out:
If flag <> 0 Then
    Console.WriteLine("Item found at index {0} in array", flag)
Else
    Console.WriteLine("Item is not found")
End If

In the above code, we used linear search technique, in the linear search we compared each element of array one by with searched item. if we found the item then assign the location of item to the flag variable and then transfer the control of program out of the loop, and print the location of item on the console screen. If item is not found in the array then print "Item is not found" message on the console screen.

VB.Net Array Programs »






Comments and Discussions!

Load comments ↻






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