I have an array of elements: int[] elem = new int[] {A, B, C}; I need to calculate the sum of ALL the combinations of those elements, where only some of the elements can be optionally selected. I will use the notation: - {0,1} mean element can be selected or not - {1} mean element is always selected. Therefore, I need to calculate the following value, which could be something like: result = {0,1}*A + {1}*B + {0,1}*C; I have tried to use nested for loops, like so: for (int i=0;i<2;i++) for (int j=1;j<2;j++) // notice I start at 1, not 0, because B is {1} for (int k=0;k<2;k++) Console.Write( i*A + j*B + k*C ); There are two problems with this: * These nested for loops are rather cumbersome * My example only has three elements, but the array could be of any size. For example, if the array had 10 elements, I would need to have 10 nested loops instead of three. I don't know to change the number of for loops based on the array size. **How would I iterate to get all sum values for all combinations in an arbitrary sized array?**