Q:

Write a program in C# Sharp to generate all possible permutations of an array using recursion

0

 Write a program in C# Sharp to generate all possible permutations of an array using recursion. 
Test Data :
Input the number of elements to store in the array [maximum 5 digits ] :3
Input 3 number of elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Expected Output :
The Permutations with a combination of 3 digits are :
123 132 213 231 321 312

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
class formPermut
{
   public void swapTwoNumber (ref int a, ref int b)
   {
      int temp = a;
      a = b;
      b = temp;
   }
   public void prnPermut (int []list, int k, int m)
   {
      int i;
      if (k == m)
      { 
         for (i = 0; i <= m; i++)
         Console.Write ("{0}",list [i]);
         Console.Write (" ");
      }
      else
         for (i = k; i <= m; i++)
         {
            swapTwoNumber (ref list [k], ref list [i]);
            prnPermut (list, k+1, m);
            swapTwoNumber (ref list [k], ref list [i]);
         }
   }
}
class RecExercise11
{
   public static void Main()
   {
       int n,i;
      formPermut test = new formPermut();
      int[] arr1 = new int[5];       
       Console.WriteLine("\n\n Recursion : Generate all possible permutations of an array :");
		Console.WriteLine("------------------------------------------------------------------");      
      
        Console.Write(" Input the number of elements to store in the array [maximum 5 digits ] :");
        n = Convert.ToInt32(Console.ReadLine());    
        Console.Write(" Input {0} number of elements in the array :\n",n);
        for(i=0;i<n;i++)
            {
	        Console.Write(" element - {0} : ",i);
	        arr1[i] = Convert.ToInt32(Console.ReadLine()); 
	        }      
        Console.Write ("\n The Permutations with a combination of {0} digits are : \n",n);
        test.prnPermut(arr1, 0, n-1);
        Console.Write ("\n\n");
   }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now