In: Computer Science
Enhance your program from Exercise 20 by continuing to ask the user for the interest rate and loan amount. Then ask the user for the amount of principle they would like to payoff each month. Make sure that the user enters a positive number that is less than the loan amount. This means that the monthly payment will change from month to month. Use this information to produce a payment chart that will include as output the Payment (Month) Number, the Payment Amount (monthly amount + interest), and the interest for that month. Make sure to calculate the Payment Amount for the last month as the remaining Loan Amount + Interest. At the end of the payment chart, output the total interest paid.
Ask user for loan amount, interest rate and monthly principle payment.
Check to make sure the monthly principle payment is less than the loan amount.
Set month counter and total interest to zero
Repeat until loan amount is less than or equal principle payment
Calculate interest as loan amount * interest rate
Calculate payment as interest + principle payment
Add interest to total interest
Increment month counter
print the month counter, payment amount and interest
Subtract principle payment from loan amount
Calculate final interest = loan amount * interest rate
Calculate final payment = loan amount + final interest
increment the month counter
Add final interest to total interest
print the month counter, final payment and final interest
print the total interest
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double loanAmount;
double interestRate;
double interestRatePerMonth;
double monthlyPayment;
double paymentPrincipal;
int months;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Enter the loan amount: ";
cin >> loanAmount;
cout << endl;
cout << "Enter the interest rate per year:
";
cin >> interestRate;
cout << endl;
interestRatePerMonth = (interestRate / 100) /
12;
cout << "Enter the monthly payment:
";
cin >> monthlyPayment;
if (monthlyPayment <= loanAmount *
interestRatePerMonth)
{
cout << "Monthly
payment is too low. The loan cannot be repaid."
<< endl;
return 1;
}
months = 0;
while (loanAmount > 0)
{
paymentPrincipal =
monthlyPayment - (loanAmount * interestRatePerMonth);
loanAmount = loanAmount
- paymentPrincipal;
months++;
}
cout << "It will take " << months
<< " months to repay the loan."
<<
endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double loanAmount;
double interestRate;
double interestRatePerMonth;
double monthlyPayment;
double paymentPrinciple;
double totalInterest = 0;
double interest;
int months;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Enter the loan amount: ";
cin >> loanAmount;
cout << endl;
cout << "Enter the interest rate per year: ";
cin >> interestRate;
cout << endl;
interestRatePerMonth = (interestRate / 100) / 12;
cout << "Enter principle amount that will paid monthly:
";
cin >> paymentPrinciple;
if (paymentPrinciple >= loanAmount)
{
cout << "Principle payment should be less than loan
amount"
<< endl;
return 1;
}
months = 0;
cout << left << setw(10) << "Month" <<
right << setw(15) << "Payment"
<< setw(15) << "Interest" << endl;
while (loanAmount > 0)
{
interest = loanAmount * interestRatePerMonth;
if(paymentPrinciple + interest > loanAmount)
monthlyPayment = loanAmount + interest;
else
monthlyPayment = paymentPrinciple + interest;
totalInterest += interest;
months++;
cout << left << setw(10) << months << right
<< setw(15) << monthlyPayment
<< setw(15) << interest << endl;
loanAmount = loanAmount - paymentPrinciple;
}
cout << "It will take " << months << " months
to repay the loan."
<< endl;
cout << "Total interest amount paid = $" <<
totalInterest << endl;
return 0;
}