In: Computer Science
C++
Write a program that outputs an amortization schedule for a given loan.
C++ Program:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void amortizationLoan(int years, double principal,double InterestRate);
int main()
{
int years;
double principal;
double InterestRate;
cout << "Please enter the Loan amount that has taken: ";
cin>> principal;
cout << "Please enter the interst: ";
cin >> InterestRate;
cout << "Please enter the number of years: ";
cin >> years;
amortizationLoan(years,principal,InterestRate);
return 0;
}
void amortizationLoan(int years, double principal,double InterestRate)
{
double periodicInterestRate = InterestRate / 12;
int NumMonths = years * 12;
double payment = principal * periodicInterestRate / (1 - pow(1 + periodicInterestRate, -NumMonths));
cout.setf(ios::fixed);
cout.precision(2);
cout << "My payment is: " << payment << endl;
double balance = principal;
double total_interest = 0;
double total_principal = 0;
int months = 0;
for (int i = 1; i <= NumMonths; i++)
{
double to_interest = balance * periodicInterestRate;
double to_principal = payment - to_interest;
total_interest += to_interest;
total_principal += to_principal;
balance -= (payment - to_interest);
months++;
}
cout<<"number of months: "<<months<<"\n";
cout<<"Interest to pay: "<<total_interest <<"\n";
cout<<"principle to pay: "<<total_principal<<"\n";
cout<<"ending balance: "<<balance<<"\n";
cout<< "Total Paid including interest: "<<total_principal+total_interest;
}
OUTPUT:
Please enter the Loan amount that has taken: 30000
Please enter the interst: 0.08
Please enter the number of years: 3
My payment is: 940.09
number of months: 36
Interest to pay: 3843.27
principle to pay: 30000.00
ending balance: -0.00
Total Paid including interest: 33843.27