// 1: [0, 0] // 2: [0, 1] // 3: [0, 2] // 4: [0, 3] // 5: [1, 1] // 6: [1, 2] // 7: [1, 3] // 8: [2, 2] // 9: [2, 3] // 10: [3, 3] using System; public static class CombinationsWithRep { private static int numberofCombos; private static int n; private static int k; private static int[] storageArr; public static void Main() { n = 4; k = 2; storageArr = new int[k]; GenCombinationsWithRep(); } private static void GenCombinationsWithRep(int index = 0, int element = 0) { if (index >= storageArr.Length) { PrintCombo(); return; } for (int i = element; i < n; i++) { storageArr[index] = i; GenCombinationsWithRep(index + 1, i); } } private static void PrintCombo() { Console.WriteLine( "{0,3}: [{1}]", ++numberofCombos, string.Join(", ", storageArr)); } }