×

VB.Net Programs

VB.Net program to find the second smallest element from the array of integers

By Nidhi Last Updated : November 15, 2024

Second smallest element in an integer array

Here, we will create an array of integers and then find the second smallest element in the array, after that print that element on the console screen.

Program/Source Code:

The source code to find the second smallest element from the array of integers is given below. The given program is compiled and executed successfully.

VB.Net code to find the second smallest element in an integer array

'VB.Net program to find the second smallest element 
'from the array of integers.

Module Module1

    Sub Main()
        Dim arr As Integer() = New Integer(5) {}
        Dim small As Integer = 0
        Dim second_small As Integer = 0
        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

        'Find the second smallest element from array
        small = arr(0)
        For i = 1 To 4 Step 1
            If (small > arr(i)) Then
                second_small = small
                small = arr(i)
            End If
        Next
        Console.WriteLine("Second smallest element in array is: {0}", second_small)
    End Sub
    
End Module

Output

Enter array elements:
Element[0]: 40
Element[1]: 50
Element[2]: 30
Element[3]: 20
Element[4]: 10
Second smallest element in array is: 20
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains a function Main().

In the Main() method, we created an array arr and two integer variables small and second_small, which 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 value of the array elements.

'Find the second smallest element from array
small = arr(0)
For i = 1 To 4 Step 1
    If (small > arr(i)) Then
        second_small = small
        small = arr(i)
    End If
Next

Console.WriteLine("Second smallest element in array is: {0}", second_small)

In the above code, we found the second smallest element from the array and then print that on the console screen.

VB.Net Array Programs »


Comments and Discussions!

Load comments ↻





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