我已经遇到的问题是,我的代码会被卡住或者在一个无限循环或将有一个堆栈溢出的问题,并开始计算过程中产生负数。C程序陷在一个无限循环
我知道这个问题是从我while循环来了,也觉得这个问题可能在于我使用的是行i = (r/12)*(b - p + temp);
公式。
但是我不确定如何解决这个问题。我的公式正试图计算12个月内每月固定利率贷款的利息,并将剩余余额印刷到屏幕上。这应该持续到余额达到0
。
#include <stdio.h>
// main function
int main()
{
// variable declarations
float r = 0.22; // interest rate
float b = 5000.0; // amount borrowed
float p; // payment amount
int m = 1;
float temp, ti = 0;
float i;
// Take in data from user
printf("Please enter the amount you wish to pay monthly: \n");
scanf("%f", &p);
printf("\n");
//display interest rate, initial balance, monthly payment
printf("r = %.2f\nb = %.1f\np = %.1f \n\n", r, b, p);
// Month by month table showing month interest due/paid and remaining balance
i = (r/12) * b;
temp = i;
printf("%d %.2f %.2f\n", m,i,b);
m++;
while (i > 0) {
i = (r/12) * (b - p + temp);
b = (b - p + temp);
ti += temp;
temp = i;
printf("%d %.2f %.2f\n",m, i, b);
m++;
}
printf("\n");
printf("total interest paid: %.2f\n", ti);
return 0;
}
请务必提及您试图用公式实现的目标。 – Haris
更新为包含该信息。 –
如果你解释变量'i'是什么会很好。 –