15,113,692 members
See more:
I developing a application in c# and MySql. Need help for creating a function to calculate penalty for delayed payment with interest.

Let A=Amount =1000, Interest for delay payment =I= 1%, Penalty=P. (Monthly Payment)

if one month delay for A=1000, P should be 10. (1000/100 *1%)

if two months delay for A=1000, P should be 10 + 20(double the Previous month Penalty).

if three months delay for A=1000, P should be 10 + 20 + 40
if four months delay for then P should be 10 + 20 + 40 + 80
if five months delay then P should be 10 + 20 + 40 + 80 + 160
if six months delay then P should be 10 + 20 + 40 + 80 + 160 + 320

I am not getting any idea to implement this.. Any expert ideas will be helpful using stored procedure (MySql) or functions in c#.

ACUBE.
Posted
Updated 1-Nov-14 21:48pm
Abhishek Pant 2-Nov-14 3:03am

you may use cases in mysql for these multiple case to calculate
```CASE [ expression ] WHEN condition_1 THEN result_1 WHEN condition_2 THEN result_2 ... WHEN condition_n THEN result_n ELSE result END```
just like you do in c#
George Jonsson 2-Nov-14 3:39am

So if you have a 36 month delay, you create 36 WHEN statements?
Maciej Los 2-Nov-14 15:18pm

Share your database structure and sample data.

## Solution 1

This is a pretty simple math operation.

Basically what you want is to loop through the number of months delay and calculate the sum of the interest for each month + the previous value doubled.

(if it is not school work it is a bit scary)
`P = A * I + 2 * P;`

C#
```double P = 0.0;
int monthsDelayed = 4;
for (int i = 0; i < monthsDelayed; i++)
{
P = A * I + 2 * P;
}```

Of course it can be done without the loop as well.
C#
```int monthsDelayed = 4;
double P = A * I * (Math.Pow(2, monthsDelayed) - 1);```

(Haven't done much math in a long time so it took me a while to see the pattern)
v3