1

我正在尝试用 C 编写一个程序来计算给定月数后的剩余贷款余额,给定余额、每月付款金额和利率。(每个月,余额增加(余额*(利率/12)),并减少支付金额。)

我计算每个月余额的代码如下:

for (i = 1; i <= n; i++) {
    loanAmount += (loanAmount * (intRate/12));
    loanAmount -= monthlyPayment;
    printf("The balance after month %d is %.2f\n", i, loanAmount);
}

我输入了一些值(loanAmount = 1000, intRate = 12, monthlyPayment = 100, n = 3),我预计第 1 个月后的结果为 910.00,第 2 个月后为 819.10,第 3 个月后为 727.29。但是,我得到了以下结果:

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

我在我的代码中做错了什么?我认为我的算法是正确的。

4

1 回答 1

2

您的利率需要为 0.12,因为此时您只是乘以 1,因此将余额加 1000,然后减去付款。

于 2012-10-24T04:15:11.193 回答