Sum of subset of 5 numbers equals 0 I have a task to print all zero subsets of 5 numbers, input from the console.
I have succeeded in implementing a working code, but it seems it is quite complex (it is like inception) and if the count of the numbers is to be greater, it would be rather pointless to use such a method.

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

class ZeroSubset
{
    static void Main()
    {
        int number;
        int[] Numbers = new int[5];
        bool result = false;
        for (int i = 0;i &lt;=4; i++)
        {
            here: ;
            Console.WriteLine("Input number {0}", i + 1);
            if (int.TryParse(Console.ReadLine(), out number))
            {
                Numbers[i] = number;
            }
            else
            {
                goto here;
            }
        }

        if (Numbers[0] == 0 &amp;&amp; Numbers[1] == 0 &amp;&amp; Numbers[2] == 0 &amp;&amp; Numbers[3] == 0 &amp;&amp; Numbers[4] == 0)
        {
            result = true;
            Console.WriteLine(String.Join("+", Numbers) + " = 0");
            return;
        }

        for (int firstNum = 0; firstNum &lt;= 3; firstNum++)
        {
            for (int secondNum = firstNum + 1; secondNum &lt;= 4; secondNum++)
            {
                if (Numbers[firstNum] + Numbers[secondNum] == 0)
                {
                    result = true;
                    Console.WriteLine("{0} + {1} = 0", Numbers[firstNum], Numbers[secondNum]);
                }
            }
        }

         for (int firstNum = 0; firstNum &lt;= 2; firstNum++)
         {
            for (int secondNum = firstNum + 1; secondNum &lt;= 3; secondNum++)
            {
                for (int thirdNum = secondNum + 1; thirdNum &lt;= 4; thirdNum++)
                {
                    if (Numbers[firstNum] + Numbers[secondNum] + Numbers[thirdNum]== 0)
                    {
                        result = true;
                        Console.WriteLine("{0} + {1} + {2} = 0", Numbers[firstNum], Numbers[secondNum], Numbers[thirdNum]);
                    }
                }
            }
        }

        for (int firstNum = 0; firstNum &lt;= 1; firstNum++)
        {
            for (int secondNum = firstNum + 1; secondNum &lt;= 2; secondNum++)
            {
                for (int thirdNum = secondNum + 1; thirdNum &lt;= 3; thirdNum++)
                {
                    for (int fourthNum = thirdNum + 1; fourthNum &lt;= 4; fourthNum++)
                    {
                        if (Numbers[firstNum] + Numbers[secondNum] + Numbers[thirdNum] + Numbers[fourthNum]== 0)
                        {
                            result = true;
                            Console.WriteLine("{0} + {1} + {2} + {3} = 0", Numbers[firstNum], Numbers[secondNum]
                                    , Numbers[thirdNum], Numbers[fourthNum]);
                        }
                    }
                }
            }
        }

        if (Numbers.Sum() == 0)
        {
            result = true;
            Console.WriteLine(String.Join("+", Numbers) + " = 0");
        }

        if (result == false)
        {
            Console.WriteLine("no zero subsets");
        }
    }
}


I am asking for a more simple approach to this matter.
Using a List&lt;int&gt; instead of int[] allows your code to not only be more dynamic but also to leverage the GetRange method of the List.  This simplifies your code to only 2 loops.

Creating the list would look something like this:

int number;
List&lt;int&gt; Numbers = new List&lt;int&gt;();
for(int i = 0; i &lt;= 4; i++)
{
    number = 0;
    bool good = false;
    while(!good)
    {
        Console.WriteLine("Input number {0}", i + 1);
        if(int.TryParse(Console.ReadLine(), out number))
        {
            Numbers.Add(number);
            good = true;
        }
        else
            Console.WriteLine("Invalid input, try again");
    }

}


The sub routine to get the subsets could look something like this:

public static List&lt;List&lt;int&gt;&gt; AllSubsets2(List&lt;int&gt; mainset, int targetsum)
{
    List&lt;List&lt;int&gt;&gt; outval = new List&lt;List&lt;int&gt;&gt;();
    for(int num2 = 0; num2 &lt; mainset.Count; num2++)
    {
        if(mainset[num2] == 0)
            outval.Add(new List&lt;int&gt;{mainset[num2]});
        int sum = mainset[num2];
        for(int num = num2+1; num &lt; mainset.Count; num++)
        {
            sum += mainset[num];
            if(sum == 0)
                outval.Add(mainset.GetRange(num2, num - num2));
        }
    }

    return outval;
}


This will return a list of all the subsets that add up to the target sum.