title: Program to print permutation and combination for the given element
body: <p>I have written the following code in PowerShell to display the permutation and combination for the given string but I am not able to achieve it by recursive functions.</p>
  
  <pre class="lang-bsh prettyprint-override"><code>function Swapping1 {
      $a = "abc"
      $b = $a.ToCharArray()
      $c = $b.Length-1
      $d = 0
      GetPer($b, $d, $c)
  }
  
  function GetPer {
      Param($list, $k, $m)
  
      [char[]] $list1 = $list[0]
  
      $k = $list[1]
      $m = $list[2]
  
      if ($k -eq $m) {
         # Write-Host $list1
      } else {
          for ($i = $k; $i -le 1; $i++) {
              $a = $list1[$k]
              $b = $list1[$i]
  
              $a, $b = $b, $a
              $k = $k+1
              Write-Host $list[0]
              GetPer($list[0], $k, $m)
              $a, $b = $b, $a
          }
      }
  }
  </code></pre>
  
  <p>The same logic when I write it in C# it is working fine.</p>
  
  <pre class="lang-cs prettyprint-override"><code>private static void Swap(ref char a, ref char b) {
      if (a == b) return;
  
      a ^= b;
      b ^= a;
      a ^= b;
  }
  
  private static void GetPermutation(char[] list, int k, int m) {
      if (k == m) {
          Console.Write(list);
          Console.WriteLine();
      } else
          for (int i = k; i &lt;= m; i++) {
              Swap(ref list[k], ref list[i]);
              GetPermutation(list, k + 1, m);
              Swap(ref list[k], ref list[i]);
          }
  }
  
  static void Main(string[] args) {
      string str = "abc";
      char[] arr = str.ToCharArray();
      int x = arr.Length - 1;
      GetPermutation(arr, 0, x);
      //GetPer(arr);
      Console.Read();
  }
  </code></pre>
  
  <p>Below is the output when I executed the PowerShell script:</p>
  
  <pre><code>PS C:\WINDOWS\system32&gt; C:\Dorababu\Power Shell\Swapping1.ps1
  a b c
  a b c
  a b c
  </code></pre>
  
  <p>C# output:</p>
  
  <p><a href="https://i.stack.imgur.com/Kzqvm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Kzqvm.png" alt="enter image description here"></a></p>