Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _02.NestedLoopsToRecursion
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- internal static class Program
- {
- public static void Main()
- {
- PrintNestedLoops(5);
- }
- private static void PrintNestedLoops(int loopCount)
- {
- IterateNestedLoops(new Stack<int>(), 0, loopCount);
- }
- private static void IterateNestedLoops(Stack<int> nestedLoops, int currentIteration, int iterationCount)
- {
- // Bottom case - reached end
- if (currentIteration == iterationCount)
- {
- Console.WriteLine(string.Join(" ", nestedLoops.Reverse()));
- return;
- }
- for (int currentNumber = 1; currentNumber <= iterationCount; currentNumber++)
- {
- nestedLoops.Push(currentNumber);
- IterateNestedLoops(nestedLoops, currentIteration + 1, iterationCount);
- nestedLoops.Pop();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement