//Jacklyn Isordia CSC5 Chapter 3, P. 146, #17
//
/**************************************************************
*
* Calculate Monthly Loan Payments
* ____________________________________________________________
* This program calculates the monthly payment for a loan using
* the formula:
*
* Pyament = Rate * (1 + Rate)^N
* -------------------- * L
* ((1 + Rate)^N - 1)
*
* Rate = monthly interest rate
* N = number of payment
* L = loan amount
* ____________________________________________________________
* INPUT
* loanAmount : total amount of loan
* annualRate : annual interest rate
* numPayments : number of payments
*
* OUTPUT
* monthlyPayment : payment due of loan
* annualRate : total amount paid
* interestPaid : total interest paid
*
**************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
float loanAmount;
float annualRate;
float monthlyRate;
int numPayments;
float monthlyPayment;
float amountPaidBack;
float interestPaid;
//
// Input Values
//
cout << "Enter loan amount: ";
cin >> loanAmount;
cout << "Enter annual interest rate (percent): ";
cin >> annualRate;
cout << "Enter nubmer of payments: ";
cin >> numPayments;
//
// Convert Interest Rate
//
monthlyRate = (annualRate / 100) /12;
//
// Compute Monthly Payment
//
monthlyPayment =
monthlyRate * pow((1 + monthlyRate), numPayments) /
(pow((1 + monthlyRate), numPayments) - 1) * loanAmount;
//
// Compute Totals
//
amountPaidBack = monthlyPayment * numPayments;
interestPaid = amountPaidBack - loanAmount;
//
// Output Result
//
cout << fixed << setprecision(2);
cout << endl;
cout << "Loan Amount: $" << loanAmount << endl;
cout << "Monthly Interest Rate: " << monthlyRate * 100 << "%" << endl;
cout << "Number of Payments: " << numPayments << endl;
cout << "Monthly Payment: $" << monthlyPayment << endl;
cout << "Amount Paid Back: $" << amountPaidBack << endl;
cout << "Interest Paid: $" << interestPaid << endl;
return 0;
}