我正在編寫計算貸款月度支付的程序。但它並沒有給出正確的答案。這裏是我的代碼:C++中的貸款還款計算
#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 . . .
正如你所看到的,貸款金額爲明顯錯誤。我如何修復我的代碼?
「沒有給出正確的答案」並不是真的告訴我們你期望它真的做什麼。你現在得到什麼,你想得到什麼? –
對不起,我將來會更加清楚。 –
只需編輯問題並添加信息即可。沒有必要抱歉,只需要修理東西!我敢打賭,如果你告訴我們關於真正的問題,那麼人們就會投票回來。 –