0
我想在C編寫一個程序來計算第一次,第二次&第三次月度支付後的剩餘餘額,給定的貸款,每月支付金額和利率。我在輸入輸入時遇到問題(浮點數),即只需要一個輸入(貸款)&顯示答案,而不考慮其他2個輸入(利率&每月支付)。來計算餘下的貸款餘額
只有當我使用浮點數(甚至在其他程序中)時纔會出現此問題。我想問問這是由於編碼還是由於其他原因。
我的代碼如下:
#include<stdio.h>
main()
{
float loan,interest,monthly_payment;
float balance_Imonth,balance_IImonth,balance_IIImonth;
printf("Enter the amount of loan: ");
scanf("%.2f",&loan);
printf("Enter the amount of interest: ");
scanf("%.2f",&interest);
printf("Enter the amount of monthly payment: ");
scanf("%.2f",&monthly_payment);
balance_Imonth=((interest/(100*12))*loan)+(loan)-(monthly_payment);
balance_IImonth=((interest/(100*12))*loan)+(balance_Imonth)- (monthly_payment);
balance_IIImonth=((interest/(100*12))*loan)+(balance_IImonth)-(monthly_payment);
printf("Balance remaining after I payment: $%.2f\n",balance_Imonth);
printf("Balance remaining after II payment: $%.2f\n",balance_IImonth);
printf("Balance remaining after III payment: $%.2f\n",balance_IIImonth);
}
在一個有點相關的說明,[它是不使用浮動與金錢打交道時,點數學是最好的主意(http://stackoverflow.com/q/3730019/274261) 。 – ArjunShankar
在問題中詢問是否找到小數點後的餘額@ArjunShankar。我沒有很好的編程技巧,我是初學者。這就是我面臨這個困難的原因。 –