//Micah Krosby CS1A Ch. 3, P. 146, # 17
/*******************************************************************************
* CALCULATE INTEREST PAID ON LOAN
* _____________________________________________________________________________
* This program calculates the amount of interest paid on a loan with a monthly
* interest rate and a number of monthly payments.
*
* FORMULAE-
* Payment = loan * (rate * (1 + rate)^times paid/((1 + rate)^times paid - 1))
* Total paid = payment * times paid
* Interest paid = total paid - loan
* _____________________________________________________________________________
*
* INPUTS-
* loan : Loan given to customer
* numPay : Number of payments made by customer
* rate : Interest rate of loan
* OUTPUTS-
* payment : Monthly payment size
* totalPaid : Total payment size
* interestPaid : Total interest earned on loan
*******************************************************************************/
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
//monthly payment formula
float monthlyPayment(float rate, unsigned short int numPay, float loan) {
return loan * rate * pow((1.0+rate), numPay) / (pow((1.0+rate), numPay)-1);
}
int main() {
float loan; //INPUT- Loan given
unsigned short int numPay; //INPUT- Number of payments
float rate; //INPUT- Interest rate monthly
float payment; //OUTPUT- Monthly payments
float totalPaid; //OUTPUT- Total paid
float interestPaid; //OUTPUT- Total interest paid on loan
//initialize input variables
cout << "Loan size?" << endl;
cin >> loan;
cout << "Monthly interest rate?" << endl;
cin >> rate;
cout << "Number of payments?" << endl;
cin >> numPay;
cout << endl;
//initialize output variables
payment = monthlyPayment(rate, numPay, loan);
totalPaid = payment * numPay;
interestPaid = totalPaid - loan;
//output
cout << setw(30) << left << "Loan Amount:" << setw(10) << "$"
<< setw(10) << right << fixed << setprecision(2) << loan << endl;
cout << setw(30) << left << "Monthly Interest Rate:" << setw(19)
<< right << rate * 100 << "%" << endl;
cout << setw(30) << left << "Number of Payments:" << setw(20)
<< right << numPay << endl;
cout << setw(30) << left << "Monthly Payments:" << setw(10) << "$"
<< setw(10) << right << fixed << setprecision(2) << payment << endl;
cout << setw(30) << left << "Amount Paid:" << setw(10) << "$"
<< setw(10) << right << totalPaid << endl;
cout << setw(30) << left << "Interest Paid:" << setw(10) << "$"
<< setw(10) <<right << interestPaid << endl;
return 0;
}