C - 付款计算。不会以0美元结束

时间:2012-01-28 23:52:51

标签: c payment calculator

我正在尝试创建一个付款计算器,但是我遇到的错误是剩余余额是负数而不是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;

1 个答案:

答案 0 :(得分:0)

你在贷款结束时两次收取利息。移动这个

if (loan < payment)
{
    payment = loan + intBal;
}

到此行之后

intBal = intRate * loan / 12;