【发布时间】:2012-10-24 04:09:53
【问题描述】:
我正在尝试用 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
我在代码中做错了什么?我认为我的算法是正确的。
【问题讨论】:
-
请不要包含图像 - 实际代码更好,因为(我们)我可以剪切和粘贴它来测试它
-
您可能想要使用一些浮点数,然后先将该利率除以 100 =P
-
对不起,我在远程终端工作,不知道如何复制/粘贴。
-
Putty 会让你举个例子
-
不开玩笑。现在那就是在 q-edit 上加倍努力。
标签: c