SHARE
TWEET

NestedLoopsToRecursion

a guest Sep 27th, 2015 201 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace _02.NestedLoopsToRecursion
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.    
  7.     internal static class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             PrintNestedLoops(5);
  12.         }
  13.  
  14.         private static void PrintNestedLoops(int loopCount)
  15.         {
  16.             IterateNestedLoops(new Stack<int>(), 0, loopCount);
  17.         }
  18.  
  19.         private static void IterateNestedLoops(Stack<int> nestedLoops, int currentIteration, int iterationCount)
  20.         {
  21.             // Bottom case - reached end
  22.             if (currentIteration == iterationCount)
  23.             {
  24.                 Console.WriteLine(string.Join(" ", nestedLoops.Reverse()));
  25.                 return;
  26.             }
  27.  
  28.             for (int currentNumber = 1; currentNumber <= iterationCount; currentNumber++)
  29.             {
  30.                 nestedLoops.Push(currentNumber);
  31.                 IterateNestedLoops(nestedLoops, currentIteration + 1, iterationCount);
  32.                 nestedLoops.Pop();
  33.             }
  34.         }
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top