Reputation:
I am trying to write a program in C to calculate the remaining loan balance after a given number of months, given the balance, monthly payment amount, and interest rate. (Each month, the balance increases by (balance * (interest rate/12)), and decreases by the payment amount.)
My code to calculate the balance for each month is as follows:
for (i = 1; i <= n; i++) {
loanAmount += (loanAmount * (intRate/12));
loanAmount -= monthlyPayment;
printf("The balance after month %d is %.2f\n", i, loanAmount);
}
I put in some values (loanAmount = 1000
, intRate = 12
, monthlyPayment = 100
, n = 3
), and I expected the result to be 910.00 after month 1, 819.10 after month 2, and 727.29 after month 3. However, I got these results instead:
Enter the loan amount:
1000
Enter the interest rate:
12
Enter the monthly payment amount:
100
Enter the number of monthly payments:
3
The balance after month 1 is 1900.00
The balance after month 2 is 3700.00
The balance after month 1 is 7300.00
What am I doing wrong in my code? I thought my algorithm was correct.
Upvotes: 1
Views: 3209
Reputation: 2708
Your interest rate needs to be .12 as your are just multiplying by 1 at the moment, therefore adding 1000 to the balance and then subtracting the payment.
Upvotes: 2