Click here to Skip to main content
15,113,692 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
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#.

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


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.

1 solution

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.

This formula should help you along in your school work.
(if it is not school work it is a bit scary)
P = A * I + 2 * P;

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.
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)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900