Reputation: 884
I am creating a small program for a homework assignment. The program runs correctly except the calculation is not correct.
The formula I am using to calculate the payment amount is:
Payment = (intRate * (1+intRate)^N / ((1+intRate)^N-1)) * L
Where "N" is the number of payments and "L" is the principal. The code I wrote to do this is:
monthlyPayment = (intRate * pow ((1 + intRate), numberPayments) / (intRate * pow ((1 + intRate), numberPayments)-1))*principal;
The complete code is below.
#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");
}
Thanks in advance for the help!
Upvotes: 0
Views: 756
Reputation: 110698
You're multiplying both the numerator and denominator by intRate
, when you should only be multiplying the numerator, according to the your equation.
You're also subtracting 1 from the result of the second pow
rather than from numberPayments
.
(intRate * pow ((1 + intRate), numberPayments)-1)
// ^ Why is this here? Wrong place ^
What you actually want is:
monthlyPayment = (intRate * pow(1+intRate, numberPayments) /
pow(1+intRate, numberPayments-1)) * principal;
Upvotes: 1
Reputation: 264571
Seems like an extra argument has been slipped in:
((1+intRate)^N-1)) * L => (intRate * pow ((1 + intRate), numberPayments)-1))*principal
// ^^^^^^^^^ Look Odd.
Additionally:
(1+intRate)^N-1
You represent this as:
pow ((1 + intRate), numberPayments)-1
I would have done that differently as:
pow ((1 + intRate), (numberPayments-1))
Upvotes: 0
Reputation: 12797
i think you have extra intRate
`(intRate * pow ((1 + intRate), numberPayments)-1))*principal;`
^^^^^^^^
Upvotes: 0