Thursday, September 25, 2025

Write a program to Generate all possible permutations of the string "ABCDEF" using C#.

 Since "ABCDEF" has 6 characters, the number of permutations = 6! = 720.

Here’s a C# program to generate and print them:

using System; using System.Collections.Generic; class Program { static void Main() { string input = "ABCDEF"; var results = new List<string>(); GetPermutations(input.ToCharArray(), 0, results); Console.WriteLine($"Total Permutations: {results.Count}"); foreach (var str in results) { Console.WriteLine(str); } } static void GetPermutations(char[] arr, int index, List<string> results) { if (index == arr.Length - 1) { results.Add(new string(arr)); } else { for (int i = index; i < arr.Length; i++) { Swap(ref arr[index], ref arr[i]); GetPermutations(arr, index + 1, results); Swap(ref arr[index], ref arr[i]); // backtrack } } } static void Swap(ref char a, ref char b) { if (a == b) return; char temp = a; a = b; b = temp; } }

🔹 How it works:

  1. Uses recursion to generate all permutations.

  2. Swaps characters, calls recursively, then backtracks to restore the original order.

  3. Stores all unique permutations in a List<string>.


🔹 Sample Output:

Total Permutations: 720 ABCDEF ABCDE F ABCDFE ABCEDF ... FEDCBA

👉 This prints all 720 unique permutations of "ABCDEF".

No comments:

Blog Archive

Don't Copy

Protected by Copyscape Online Plagiarism Checker

Pages