我正在编写一个程序,用于计算贷款的每月付款。但它没有给出正确的答案。这是我的代码:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt;
int NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = YearlyInt / 12;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: " << Payment << endl;
AmountPaid = Payment * 36;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
}
该程序使用以下公式:
MonthlyInt * pow(1 + MonthlyInt, NumPayments) * LoanAmount
Payment = ---------------------------------------------------------------
pow(1 + MonthlyInt, NumPayments) - 1
这是我作为输出得到的:
Enter the loan amount (LoanAmount) --> 10000
Enter the YEARLY interest rate as a percentage --> 12
Enter number of payments --> 36
Loan amount: 10000
Yearly Interest Rate: 12%
Number of Payments: 36
Monthly Payment: 10000
Amount Paid Back: 360000
Interest Paid: 350000
Program Over
Press Enter to end -->
Press any key to continue . . .
正如您所看到的,贷款金额显然是错误的。我该如何修复我的代码?
答案 0 :(得分:1)
这是一个正确计算付款的程序,假设如下:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt;
int NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of monthly payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Monthly Payments: " << NumPayments << endl;
MonthlyInt = pow( 1 + YearlyInt/100, 1.0/12 );
Payment = LoanAmount * pow( MonthlyInt, NumPayments ) *
( MonthlyInt - 1 ) /
( pow( MonthlyInt, NumPayments ) - 1 );
cout << "Monthly Payment: " << Payment << endl;
AmountPaid = Payment * NumPayments;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
return 0;
}
答案 1 :(得分:1)
第1步:由于复利的影响,MonthlyInt
不等于YearlyInt / 12
。用于在较小周期的速率和较大周期的等效速率之间进行转换的通用公式是:(1 + r)^ n = 1 + R.因此在这种情况下,r = MonthlyInt并且R = YearlyInt。因此,第一项业务是改变
从:
MonthlyInt = YearlyInt / 12;
为:
MonthlyInt = pow ( (1.0 + YearlyInt) , (1.0/NumPayments) ) - 1.0; // note decimals!
步骤2:添加打印MonthlyInt
的行,以便验证计算结果。 :)
第3步:将AmountPaid = Payment * 36;
更改为AmountPaid = Payment * NumPayments;
步骤4:(可选)添加美元符号并清除小数。
我们必须添加标头#include<iomanip>
,然后使用cout << setprecision(n) << fixed << whateverVariable
设置小数位数,其中n
等于您想要的小数位数。
修订代码:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double YearlyInt = -1, LoanAmount = -1, Payment = -1, AmountPaid = -1, MonthlyInt = -1;
int NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a decimal number (e.g. 3.25% as .0325) --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: $" << setprecision(2) << fixed << LoanAmount << endl;
cout << "Yearly Interest Rate: " << setprecision(3) << YearlyInt * 100 << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = pow ( (1.0 + YearlyInt) , (1.0/NumPayments) ) - 1.0;
cout << "MonthlyInt: " << MonthlyInt*100 << "%" << endl;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: $" << setprecision(2) << Payment << endl;
AmountPaid = Payment * NumPayments;
cout << "Amount Paid Back: $" << AmountPaid << endl;
cout << "Interest Paid: $" << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
}
假设:贷款的免费APR为YearlyInt
每月复利,每月付款,第一笔付款适用于贷款发生当月的最后一天,以及所有“准时”付款(无论是由贷款定义的)都应用,如同在适用期间的最后一天支付一样。
答案 2 :(得分:0)
程序是否应该计算复利或简单利息?
您的计算似乎不正确。您计算每月利率,这使它看起来像简单的利息,但您使用pow,这表明与复利有关。你应该考虑一下。
答案 3 :(得分:0)
有一些问题:
您输入百分比的比率,因此请将它们转换为十进制数字:MonthlyInt/100.0
您的付款数量应该是固定的,或由用户输入。现在它首先被读入,但是代码中使用了36个。它应该用适当的变量替换。
小心整数除法。目前没有错误,但为了避免这种情况,如果你想确定你有漂浮物,请使用1.0和100.0而不是1和100。
确保你的数学是正确的。事实上,这应该是你做的第一件事。这是一个编程网站,所以这里是偏离主题的。
(可选)传统上,变量名称不应以大写字母开头。
答案 4 :(得分:0)
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt, NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = (YearlyInt/100.0) / 12;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: " << Payment << endl;
AmountPaid = Payment * 36;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
只需切换MonthlyInt = YearlyInt / 12;到MonthlyInt =(YearlyInt / 100.0)/ 12;
答案 5 :(得分:0)
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt, NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = (YearlyInt/100.0) / 12;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: " << Payment << endl;
//correction to Amount paid Value
AmountPaid = Payment * NumPayments;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
}
答案 6 :(得分:0)
如果您不想使用pow函数,则可以直接自己计算级数的几何和和n个项(pow()来自几何和和项)。请在此处查看证明:https://mortgagecalculator.mes.fm/amortization-formula-proof
--module-path "C:\your\path\javafx-sdk-12.0.1\lib" --add-modules javafx.controls,javafx.fxml