VB.Net program to cyclically permute the elements of an integer array

Here, we are going to learn how to cyclically permute the elements of an integer array in VB.Net?
Submitted by Nidhi, on December 10, 2020 [Last updated : March 06, 2023]

Cyclically permute the elements of an integer array

Here, we will create an array of integers and then read elements from the user. After we will permute the array element cyclically.

Program/Source Code:

The source code to cyclically permute the elements of an integer array is given below. The given program is compiled and executed successfully.

VB.Net code to cyclically permute the elements of an integer array

'VB.Net program to Cyclically Permute the Elements of an Array.

Module Module1
    Sub Main()
        Dim arr As Integer() = New Integer(5) {}
        Dim temp 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

        temp = arr(0)

        Console.WriteLine("Array elements before Cyclically Permutation: ")
        For i = 0 To 4 Step 1
            Console.Write("{0} ", arr(i))
        Next
        Console.WriteLine()

        For i = 0 To 4 Step 1
            If i = 4 Then
                arr(i) = temp
            Else
                arr(i) = arr(i + 1)
            End If
        Next

        Console.WriteLine("Array elements after Cyclically Permutation: ")
        For i = 0 To 4 Step 1
            Console.Write("{0} ", arr(i))
        Next
        Console.WriteLine()
    End Sub
End Module

Output

Enter array elements:
Element[0]: 1
Element[1]: 2
Element[2]: 3
Element[3]: 4
Element[4]: 5
Array elements before Cyclically Permutation:
1 2 3 4 5
Array elements after Cyclically Permutation:
2 3 4 5 1
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 a variable temp which is 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.

For i = 0 To 4 Step 1
    If i = 4 Then
        arr(i) = temp
    Else
        arr(i) = arr(i + 1)
    End If
Next 

Console.WriteLine("Array elements after Cyclically Permutation: ")
For i = 0 To 4 Step 1
    Console.Write("{0} ", arr(i))
Next
Console.WriteLine()

In the above code, we cyclically permute the elements of the array and then print them on the console screen.

VB.Net Array Programs »





Comments and Discussions!

Load comments ↻





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