gpt4 book ai didi

c - 找到无法偿还贷款的确切金额(避免无限循环)

转载 作者:行者123 更新时间:2023-11-30 15:39:45 25 4
gpt4 key购买 nike

这是我的一个简单的 C 程序代码,它将计算支付的利息贷款。我对代码没有任何疑问。但我无法找到一种方法来找到无法还清贷款的确切付款金额(该金额将导致无限循环)。我只知道金额应该在41.7左右。有一个聪明的方法来做到这一点吗?谢谢

#include <stdio.h>
#include <stdlib.h>

int main()
{
float p;
float i;
float temp, ti = 0;
int a = 1;

printf("Please enter your monthly payment: ");
scanf("%f", &p);
printf("\n");

float r = 0.25;
float b = 2000.0;

printf("r = %.2f\nB = %.1f\nP = %.1f \n\n", r, b, p);

i = (r/12) * b;
temp = i;
printf("%d %.2f %.2f\n", a, i, b);
a ++;

while(i > 0)
{
i = (r/12) * (b - p + temp);
b = (b - p + temp);
ti += temp;
temp = i;
printf("%d %.2f %.2f\n", a, i, b);
a ++;
}

printf("\n");
printf("total interest paid: %.2f\n", ti);

return 0;
}

最佳答案

当每月还款额小于每月利息值时,程序将进入无限循环。在给定的代码中,

i = (r/12) * (b - p + temp);

确定无限循环条件。

让循环终止

p > temp
p > (r / 12) * b

替换值给出,

p > (0.25 / 12) * 2000
p > 41.66

关于c - 找到无法偿还贷款的确切金额(避免无限循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com