我正在为家庭作业创建一个小程序。程序运行正常,但计算不正确。
我用来计算付款金额的公式是:
付款 = (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");
}
在此先感谢您的帮助!