Question: In c# how do I evenly divide 100 into 7?

So the result would be 

1. 16
2. 14
3. 14
4. 14
5. 14
6. 14
7. 14

The code below is incorrect as all 7 values are set to 15 (totalling 105).

```
        double [] vals = new double[7];
        for (int i = 0; i < vals.Length; i++)
        {
            vals[i] = Math.Ceiling(100d / vals.Length);
        }

```

Is there an easy way to do this in c#?

Thanks

Answer: 
The algorithm is this

1. Divide 100 by 7, put the result in X
2. Get the highest even number below X and put this in Y.
3. Multiply Y by 7 and put the answer in Z.
4. Take Z away from 100.

The answer is then 6 lots of Y plus whatever the result of step 4 was.

This algorithm may only work for this specific instance.

I'm sure you can write that in C#
Not sure why you are working with doubles but wanting integer division semantics.

```
    double input = 100;
    const int Buckets = 7;
    double[] vals = new double[Buckets];
    for (int i = 0; i < vals.Length; i++)
    {
        vals[i] = Math.Floor(input / Buckets);
    }
    double remainder = input % Buckets;
    // give all of the remainder to the first value
    vals[0] += remainder;

```

example for ints with more flexibility, 

```
    int input = 100;
    const int Buckets = 7;
    int [] vals = new int[Buckets];
    for (int i = 0; i < vals.Length; i++)
    {
        vals[i] = input / Buckets;
    }
    int remainder = input % Buckets;
    // give all of the remainder to the first value
    vals[0] += remainder;

    // If instead  you wanted to distribute the remainder evenly, 
    // priority to first
    for (int r = 0; r < remainder;r++)
    {
        vals[r % Buckets] += 1;
    }

```

It is worth pointing out that the double example **may not be numerically stable** in that certain input values and bucket sizes could result in leaking fractional values.