// ----------------------------------------------- all combinations 0 ------------------------------ // I have 5 integer numbers with positive and negative values. Please write a program on C# to find all combinations between the 5 numbers which are 0? using System; using System.Collections.Generic; public class ZeroSumCombinations { public static void Main() { int[] numbers = { 2, -2, 3, -3, 4 }; // Example input // Pairs for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { if (numbers[i] + numbers[j] == 0) { Console.WriteLine($"Pair: {numbers[i]}, {numbers[j]}"); } } } // Triples for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { for (int k = j + 1; k < 5; k++) { if (numbers[i] + numbers[j] + numbers[k] == 0) { Console.WriteLine($"Triple: {numbers[i]}, {numbers[j]}, {numbers[k]}"); } } } } // Quadruples for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { for (int k = j + 1; k < 5; k++) { for (int l = k + 1; l < 5; l++) { if (numbers[i] + numbers[j] + numbers[k] + numbers[l] == 0) { Console.WriteLine($"Quadruple: {numbers[i]}, {numbers[j]}, {numbers[k]}, {numbers[l]}"); } } } } } // Quintuples if (numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4] == 0) { Console.WriteLine($"Quintuple: {numbers[0]}, {numbers[1]}, {numbers[2]}, {numbers[3]}, {numbers[4]}"); } } } // ----------------------------- Above without arrays ------------------------ using System; public class ZeroSumCombinations { public static void Main() { int a = 2, b = -2, c = 3, d = -3, e = 4; // Example input // Pairs if (a + b == 0) Console.WriteLine($"Pair: {a}, {b}"); if (a + c == 0) Console.WriteLine($"Pair: {a}, {c}"); if (a + d == 0) Console.WriteLine($"Pair: {a}, {d}"); if (a + e == 0) Console.WriteLine($"Pair: {a}, {e}"); if (b + c == 0) Console.WriteLine($"Pair: {b}, {c}"); if (b + d == 0) Console.WriteLine($"Pair: {b}, {d}"); if (b + e == 0) Console.WriteLine($"Pair: {b}, {e}"); if (c + d == 0) Console.WriteLine($"Pair: {c}, {d}"); if (c + e == 0) Console.WriteLine($"Pair: {c}, {e}"); if (d + e == 0) Console.WriteLine($"Pair: {d}, {e}"); // Triples if (a + b + c == 0) Console.WriteLine($"Triple: {a}, {b}, {c}"); if (a + b + d == 0) Console.WriteLine($"Triple: {a}, {b}, {d}"); if (a + b + e == 0) Console.WriteLine($"Triple: {a}, {b}, {e}"); if (a + c + d == 0) Console.WriteLine($"Triple: {a}, {c}, {d}"); if (a + c + e == 0) Console.WriteLine($"Triple: {a}, {c}, {e}"); if (a + d + e == 0) Console.WriteLine($"Triple: {a}, {d}, {e}"); if (b + c + d == 0) Console.WriteLine($"Triple: {b}, {c}, {d}"); if (b + c + e == 0) Console.WriteLine($"Triple: {b}, {c}, {e}"); if (b + d + e == 0) Console.WriteLine($"Triple: {b}, {d}, {e}"); if (c + d + e == 0) Console.WriteLine($"Triple: {c}, {d}, {e}"); // Quadruples if (a + b + c + d == 0) Console.WriteLine($"Quadruple: {a}, {b}, {c}, {d}"); if (a + b + c + e == 0) Console.WriteLine($"Quadruple: {a}, {b}, {c}, {e}"); if (a + b + d + e == 0) Console.WriteLine($"Quadruple: {a}, {b}, {d}, {e}"); if (a + c + d + e == 0) Console.WriteLine($"Quadruple: {a}, {c}, {d}, {e}"); if (b + c + d + e == 0) Console.WriteLine($"Quadruple: {b}, {c}, {d}, {e}"); // Quintuples if (a + b + c + d + e == 0) Console.WriteLine($"Quintuple: {a}, {b}, {c}, {d}, {e}"); } } // You have 6 types of coins with values 1, 2, 5, 10, 20 and 50 cents. Write an algorithm on C# to find all combinations giving 100 cents (one dollar) // -------------------------- Coins ------------------------------------------------- using System; using System.Collections.Generic; public class CoinCombinations { private static readonly int[] Coins = { 1, 2, 5, 10, 20, 50 }; private static readonly List> Combinations = new List>(); public static void Main() { FindCombinations(new List(), 100); foreach (var combination in Combinations) { Console.WriteLine(string.Join(" ", combination)); } } public static void FindCombinations(List currentCombination, int remaining) { if (remaining == 0) { Combinations.Add(new List(currentCombination)); return; } if (remaining < 0) { return; } for (int i = 0; i < Coins.Length; i++) { if (currentCombination.Count == 0 || Coins[i] >= currentCombination[currentCombination.Count - 1]) { currentCombination.Add(Coins[i]); FindCombinations(currentCombination, remaining - Coins[i]); currentCombination.RemoveAt(currentCombination.Count - 1); } } } }