我正在尝试创建一个付款计算器,但是遇到一个错误,其中余额为负而不是$ 0。这是代码:
int main()
{
double selling, down, intRate, intBal, toBal, payment, loan, loanN, d;
int counter, i;
printf("Cost of item: ");
scanf("%lf", &selling);
printf("Down payment on item: ");
scanf("%lf", &down);
printf("Interest rate: ");
scanf("%lf", &intRate);
printf("Monthly payment: ");
scanf("%lf", &payment);
d= selling * 100.0;
i = d + 0.5;
selling = i / 100.0;
d= down * 100.0;
i = d + 0.5;
down = i / 100.0;
d= payment * 100.0;
i = d + 0.5;
payment = i / 100.0;
counter = 1;
loan = selling - down;
printf("--------------------------------------------------------------\n");
printf("Selling Price = %8.2lf\n", selling);
printf("Down Payment = %8.2lf\n", down);
printf("Amount of Loan = %8.2lf\n", loan);
printf("Annual Interest Rate = %8.2lf\n", intRate);
printf("Monthly Payment = %8.2lf\n", payment);
printf("--------------------------------------------------------------\n");
printf("Pay # Old Balance Payment Interest To Balance New Balance\n");
printf("----- ----------- ------- -------- ---------- -----------\n");
while (loan > 0)
{
if (loan < payment)
{
payment = loan + intBal;
}
printf("%5i %11.2lf %7.2lf ", counter, loan, payment);
intBal = intRate * loan / 12;
printf("%8.2lf ", intBal);
toBal = payment - intBal;
printf("%10.2lf ", toBal);
loanN = loan - toBal;
loan = loanN;
printf("%11.2lf\n", loanN);
counter++;
}
return 0;
最佳答案
您需要在贷款期末两次向他们收取利息。移动这个
if (loan < payment)
{
payment = loan + intBal;
}
在此行之后
intBal = intRate * loan / 12;