我在此代码中遇到问题:输出结果始终与主体具有相同的值。为什么呢?
#include <math.h>
#include <conio.h>
void main()
{
float principal, rate, total_amount, interest, i, j;
int time, year, quarterly, x, y;
printf("Enter Principal : ");
scanf("%f", &principal);
printf("Enter Interest Rate : ");
scanf("%f", &rate);
printf("Enter Time : ");
scanf("%d", &time);
for(x = 1; x <= time; x++)
{
i = rate / 400;
total_amount = principal * pow(1 + (i), x); //total_amount = total_end
interest = total_amount - principal;
printf("%d\t\t %.2f\t\t %.2f\t\t %.2f\n", x, principal, interest, total_amount);
}
getch();
}
答案 0 :(得分:0)
我不确定我是否正确理解了单行问题陈述,但IMO,您没有更新下一次迭代的主体。
你应该使用
for(x = 1; x <= time; x++)
{
i = rate / 400;
total_amount = principal * pow(1 + (i), x); //total_amount = total_end
interest = total_amount - principal;
printf("%d\t\t %.2f\t\t %.2f\t\t %.2f\n", x, principal, interest, total_amount);
principal = total_amount ; //see here
}