For loops and recusion (Part 2)

Recursion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void print(List<int> t)
        {
            for (int i = 0; i < t.Count; i++)
            {
                Console.Write(t[i] + " ");
            }
        }

        static void addToList(List<List<int>> list, ref List<int> temp, int[] t, int j, int cDepth, int maxDepth)
        {
            for (int i = j; i < t.Length; i++)
            {
                temp.Add(t[i]);
                if (cDepth == maxDepth)
                {
                    list.Add(temp);
                    temp = new List<int>(temp);
                    temp.RemoveAt(temp.Count - 1);
                }
                if (cDepth + 1 <= maxDepth)
                {
                    addToList(list, ref temp, t, i + 1, cDepth + 1, maxDepth);
                    temp.RemoveAt(temp.Count - 1);
                }
            }
        }

        static void Main(string[] args)
        {
            List<List<int>> list = new List<List<int>>();
            List<int> temp = new List<int>(10);

            int[] t = { 1, 2, 3 };
            for (int i = 0; i < t.Length; i++)
                addToList(list, ref temp, t, 0, 0, i);

            foreach (List<int> e in list)
            {
                print(e);
                Console.WriteLine();
            }
        }
    }
}

Output:

1
2
3
1 2
1 3
2 3
1 2 3

Nested for loop

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void print(List<int> t)
        {
            for (int i = 0; i < t.Count; i++)
            {
                Console.Write(t[i] + " ");
            }
        }

        static void Main(string[] args)
        {
            int [] arr = {1,2,3};
            List<List<int>> list = new List<List<int>>();

            for (int i = 1; i < (1 << arr.Length); i++)
            {
                List<int> temp = new List<int>();
                for (int j = 0; j < arr.Length; j++)
                {
                    if ((i & (1 << j)) > 0)
                    {
                        temp.Add(arr[j]);
                    }
                }
                if (temp.Count > 0)
                    list.Add(temp);
            }

            foreach (List<int> e in list)
            {
                print(e);
                Console.WriteLine();
            }
        }
    }
}

Output:

1
2
1 2
3
1 3
2 3
1 2 3

For loops and recursion

Example 1
Nested for loop

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static int depth = 4;
        static void funcFor()
        {
            for (int i = 0; i < depth; i++)
            {
                for (int j = 0; j < depth; j++)
                {
                    for (int k = 0; k < depth; k++)
                    {
                        for (int l = 0; l < depth; l++)
                        {
                            Console.WriteLine(i + " " + j + " " + k + " " + l);
                        }
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            funcFor();
        }
    }
}

Output:

0 0 0
0 0 1
0 0 2
0 0 3
0 1 1
0 1 2
0 1 3
0 2 2
0 2 3
0 3 3
1 1 1
1 1 2
1 1 3
1 2 2
1 2 3
1 3 3
2 2 2
2 2 3
2 3 3
3 3 3

Same functionality but with recursion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static int[] arr1 = { 1, 2, 3 };
        
        static void print(int[] a)
        {
            for (int i = 0; i < a.Length; i++)
                Console.Write(a[i] + " ");
            Console.WriteLine();
        }

        static void funcR(int j, int depth, int maxDepth, int maxLength)
        {
            for (int i = j; i <= maxLength; i++)
            {
                arr1[depth] = i;
                if (depth == maxDepth)
                    print(arr1);
                if (depth + 1 <= maxDepth)
                    funcR(i, depth + 1, maxDepth, maxLength);
            }
        }

        static void Main(string[] args)
        {
            funcR(0, 0, arr1.Length - 1, arr1.Length);
        }
    }
}