我正在为家庭作业创作一个小程序。程序运行正常,但计算不正确。
我用来计算付款金额的公式是:
付款=(intRate *(1 + intRate)^ N /((1 + intRate)^ N-1))* L
其中“N”是付款次数,“L”是本金。我写的代码是:
monthlyPayment = (intRate * pow ((1 + intRate), numberPayments) / (intRate * pow ((1 + intRate), numberPayments)-1))*principal;
完整的代码如下。
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double principal, intRate, paybackAmount, interestPaid, monthlyPayment;
int numberPayments;
// Change the panel color.
system ("color F0");
cout << "\n";
cout << "This application will calculate your loan amounts." << endl;
cout << "Please enter the data below." << endl;
cout << "\n";
cout << "Loan Amount: ";
cin >> principal;
cout << "Monthly Interest Rate: ";
cin >> intRate;
cout << "Number of Payments: ";
cin >> numberPayments;
cout << "\n\n\n";
monthlyPayment = (intRate * pow ((1 + intRate), numberPayments) / (intRate * pow ((1 + intRate), numberPayments)-1))*principal;
paybackAmount = monthlyPayment * numberPayments;
cout << fixed << setprecision(2) << showpoint << left << setw(24) << "Loan Amount:" << "$" << setw(11) << right << principal << endl;
cout << fixed << setprecision(1) << showpoint<< left << setw(24) << "Monthly Interest Rate:" << setw(11) << right << intRate << "%" << endl;
cout << fixed << setprecision(0) << left << setw(24) << "Number of Payments:" << setw(12) << right << numberPayments << endl;
cout << fixed << setprecision(2) << showpoint<< left << setw(24) << "Monthly Payment:" << "$" << setw(11) << right << monthlyPayment << endl;
cout << fixed << setprecision(2) << showpoint<< left << setw(24) << "Amount Paid Back:" << "$" << setw(11) << right << paybackAmount << endl;
cout << fixed << setprecision(2) << showpoint<< left << setw(24) << "Interest Paid:" << "$" << right << setw(11) << paybackAmount - principal << "\n\n" << endl;
system("pause");
}
提前感谢您的帮助!
答案 0 :(得分:1)
你将分子和分母乘以intRate
,当你只应该乘以分子时,根据你的等式。
您还要从第二个pow
的结果中减去1,而不是从numberPayments
减去。
(intRate * pow ((1 + intRate), numberPayments)-1)
// ^ Why is this here? Wrong place ^
你真正想要的是:
monthlyPayment = (intRate * pow(1+intRate, numberPayments) /
pow(1+intRate, numberPayments-1)) * principal;
答案 1 :(得分:0)
我认为你有额外的intRate
`(intRate * pow ((1 + intRate), numberPayments)-1))*principal;`
^^^^^^^^
答案 2 :(得分:0)
看起来像一个额外的论点已被插入:
((1+intRate)^N-1)) * L => (intRate * pow ((1 + intRate), numberPayments)-1))*principal
// ^^^^^^^^^ Look Odd.
此外:
(1+intRate)^N-1
您将此表示为:
pow ((1 + intRate), numberPayments)-1
我会以不同的方式做到:
pow ((1 + intRate), (numberPayments-1))