//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here IEnumerable GetCombinations(int[] set,int sum,string values) { for (int i = 0; i < set.Length; i++) { int left = sum - set[i]; string vals = set[i] + "," + values; if (left == 0) { yield return vals; } else { int[] possible = set.Take(i).Where(n => n <= sum).ToArray(); if (possible.Length > 0) { foreach (string s in GetCombinations(possible, left, vals)) { yield return s; } } } } } int[] set2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; foreach (string s in GetCombinations(set2, 18, "")) { Console.WriteLine(s); } } } }