ADVERTISEMENT
ADVERTISEMENT

VB.Net program to reverse an integer array

Here, we are going to learn how to reverse an integer array in VB.Net?
Submitted by Nidhi, on December 07, 2020

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 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 »



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.