×

VB.Net Programs

VB.Net program to reverse an integer array

By Nidhi Last Updated : November 16, 2024

Reverse an integer array

Here, we will create an array of integers and then assign the elements of the array into reverse order into another array.

Program/Source Code:

The source code to reverse an integer array is given below. The given program is compiled and executed successfully.

VB.Net code to reverse an integer array

'VB.Net program to reverse an integer array.

Module Module1

    Sub Main()
        Dim arr As Integer() = New Integer(5) {}
        Dim rev_arr As Integer() = New Integer(5) {}

        Dim i As Integer = 0
        Dim j 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

        'Assign reverse elements of array "arr" into "rev_arr".
        For i = 4 To 0 Step -1
            rev_arr(j) = arr(i)
            j = j + 1
        Next

        Console.WriteLine("Reversed array: ")
        For i = 0 To 4 Step 1
            Console.Write("{0} ", rev_arr(i))
        Next
        Console.WriteLine()
    End Sub
    
End Module

Output

Enter array elements:
Element[0]: 10
Element[1]: 20
Element[2]: 3
Element[3]: 4
Element[4]: 5
Reversed array:
5 4 3 20 10
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 two arrays arr and rev_arr of five elements, and two integer variables I and j 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 arr from the user.

'Assign reverse elements of array "arr" into "rev_arr".
For i = 4 To 0 Step -1
    rev_arr(j) = arr(i)
    j = j + 1
Next

Console.WriteLine("Reversed array: ")
For i = 0 To 4 Step 1
    Console.Write("{0} ", rev_arr(i))
Next

In the above code, we assigned the elements of array arr into reverse order into array rev_arr, after we printed the reversed array rev_arr on the console screen.

VB.Net Array Programs »


Comments and Discussions!

Load comments ↻





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