ADVERTISEMENT
ADVERTISEMENT

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

Here, we are going to learn how to find the second largest element from the array of integers in VB.Net?
Submitted by Nidhi, on December 07, 2020

Here, we will create an array of integers and then read elements from the user after that find the second largest element from the array and print that element on the console screen.

Program/Source Code:

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

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

Module Module1

    Sub Main()
        Dim arr As Integer() = New Integer(5) {}
        Dim first_large As Integer = 0
        Dim second_large As Integer = 0

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

            If (first_large < arr(i)) Then
                second_large = first_large
                first_large = arr(i)
            End If
        Next
        Console.WriteLine("Second Largest element in array is: {0}", second_large)
    End Sub
    
End Module

Output:

Enter array elements:
Element[0]: 10
Element[1]: 12
Element[2]: 13
Element[3]: 15
Element[4]: 16
Second Largest element in array is: 15
Press any key to continue . . .

Explanation:

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

Dim arr As Integer() = New Integer(5) {}
Dim first_large As Integer = 0
Dim second_large As Integer = 0

In the Main() we created an array arr of five elements and two integer variables first_large and second_large, which are initialized with 0.

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

    If (first_large < arr(i)) Then
        second_large = first_large
        first_large = arr(i)
    End If
Next
Console.WriteLine("Second Largest element in array is: {0}", second_large)

In the above code, we read the elements of the array from the user and then find the second largest element from the array and then print the largest element on the console screen.

VB.Net Array Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.