How to generate all the possible arrangements of 1,2,3
(for eg:..123,132,231,231,312,321.) using VB.net
i should get an generalised solution suitable for n numbers
How to generate all the possible arrangements of 1,2,3
(for eg:..123,132,231,231,312,321.) using VB.net
i should get an generalised solution suitable for n numbers
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
AlanRank #669 · 3.6K reputationPosted Sep 24, 2008, 11:32 AM EDT
This code will generate and display all possible arrangements of the numbers 1,2,3:
Imports System
Imports System.Collections.Generic
Module Permutations
Sub Main()
Dim perms As New List(Of Integer())
For i As Integer = 1 To 3
For j As Integer = 1 To 3
If i = j Then Continue For
For k As Integer = 1 To 3
If k <> i AndAlso k <> j Then
perms.Add(New Integer(2){i,j,k})
End If
Next k
Next j
Next i
Console.WriteLine("All possible arrangements of {1,2,3} are:")
Console.WriteLine()
For Each perm As Integer() in perms
Console.Write("{")
For i As Integer = 0 To 2
Console.Write(perm(i))
If i < 2 Then Console.Write(",")
Next i
Console.WriteLine("}")
Next perm
End Sub
End Module
However, as this sounds like homework, you'll have to do the general case of 'n' numbers yourself to get some benefit from this. To avoid having 'n' For statements, I'd look for a recursive solution.