我试图为学校编写这个代码,我绝对坚持我做错了,如果有人能指出我正确的方向,那将会有所帮助。尽力学习。 我的程序没有计算出,在每月末,在扣除付款后,然后添加利息时,欠多少钱。 它只显示相同的值。
#include<stdio.h>
int main()
{
float loan;
float interest;
int n;
float outstanding;
float outstanding2;
float princeable;
float payment;
printf("\nEnter the amount of the loan: \n ");
scanf("%f" , &loan);
printf("\nEnter monthly interest percentage\n ");
scanf("%f" , &interest);
printf("\nEnter monthly payments: \n ");
scanf("%f" , &payment);
printf("\nEnter number of monthly Payments: \n ");
scanf("%i" , &n);
while (n >= 0) {
outstanding = (loan - payment);
outstanding = (outstanding * (1 + (interest/100)));
printf("\Outstanding Balance after %i =%.2f\n\n", n, outstanding);
n--;
}
return 0;
}
答案 0 :(得分:2)
在每次迭代中,您应该根据其先前的值计算outstanding
,而不是根据初始贷款的价值计算,因为您还需要支付利息利息。
outstanding = loan;
while (n > 0) {
outstanding = (outstanding - payment);
outstanding = (outstanding * (1 + (interest/100)));
printf("\Outstanding Balance after %i =%.2f\n\n", n, outstanding);
n--;
}
答案 1 :(得分:0)
这一行:
outstanding = (loan - payment);
循环中的不正确,因为它每次都以初始loan
值开头。它应该是:
outstanding = (outstanding - payment);
在进入循环之前,您还必须将outstanding
设置为loan
。
最重要的是,你有一个太多的循环迭代,以及\O
字符串中的非法转义序列printf
。