Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program to be written in C++ When you borrow money to buy a house, a car, or for

ID: 3753309 • Letter: P

Question

Program to be written in C++

When you borrow money to buy a house, a car, or for some other purpose, you repay the loan by making periodic payments over a certain period of time.

Of course, the lending company will charge interest on the loan. Every periodic payment consists of the interest on the loan and the payment toward the principal amount.

To be specific, suppose that you borrow $1,000 at an interest rate of 7.2% per year and the payments are monthly. Suppose that your monthly payment is $25. Now, the interest is 7.2%per year and the payments are monthly, so the interest rate per month is 7.2/12 = 0.6%. The first month’s interest on $1,000 is 1000 X 0.006 = 6.

Because the payment is $25 and the interest for the first month is $6, the payment toward the principal amount is 25 - 6 = 19. This means after making the first payment, the loan amount is 1,000 - 19 = 981.

For the second payment, the interest is calculated on $981. So the interest for the second month is 981 X 0.006 = 5.886, that is, approximately $5.89. This implies that the payment toward the principal is 25 - 5.89 = 19.11 and the remaining balance after the second payment is 981 - 19.11 = 961.89. This process is repeated until the loan is paid.

Instructions

Write a program that accepts as input:

The loan amount

The interest rate per year

The monthly payment.

(Enter the interest rate as a percentage. For example, if the interest rate is 7.2% per year, then enter 7.2.)

The program then outputs the number of months it would take to repay the loan.

(Note that if the monthly payment is less than the first month’s interest, then after each payment, the loan amount will increase.)

In this case, the program must warn the borrower that the Monthly payment is too low. The loan cannot be repaid.

Explanation / Answer

Basic Things to be noted first writing C++ Code, Need to include first correctly,

For Math Calculation: #include<cmath> is needed

For IOManipulation: #include<iomanip> is needed

Code Should be very Ellaborative and Informative to the End-User So keep code member variables related to the questions.

Code Snippet:

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

int main()

{

double loanAmt = -1, yearInt = -1, monthInt = -1, monthPay = -1,calMonthPay=-1,totalAmt=-1,actualAmt=-1,excessAmt=-1;

int numPayments = 12; //1 Year Period

cout<<"Enter the Loan Amount -->";

cin>>loanAmt;

cout<<"Enter the Yearly Interest Rate -->";

cin>>yearInt;

cout<<"Enter Monthly Loan Paid -->";

cin>>monthPay;

if(monthPay == -1) //monthPay is not given exit the code

{

cout<<"Monthly Payment cant be mentioned -1";

return 0;

}

cout<<"Loan Amount: $"<<setprecision(2)<<fixed<<loanAmt<<endl;

cout<<"Yearly Interest Rate"<<setprecision(3)<<yearInt * 100<<"%"<<endl;

monthInt = pow( (1.0 + yearInt) , (1.0/numPayments) ) - 1.0;

cout<<"Monthly Interest Rate: "<<monthInt * 100 << "%" <<endl;

calMonthPay = monthInt * pow (( 1+ monthInt ), numPayments) / (pow(( 1 + monthInt), numPayments) -1) * loanAmt;

cout<< "Caluculated Monthly Payment : $" <<setprecision(2)<<calMonthPay <<endl;

if(monthPay <= calMonthPay)

{

cout<<"Warning: Paid Monthly Payment is Less than the Calculated Monthy Payment The Loan cannot be repaid !!!"<<endl;

}

totalAmt = calMonthPay * numPayments;

actualAmt = monthPay * numPayments;

excessAmt = (totalAmt - actualAmt);

cout<<"Total Amount Paid to be paid in 1Year is: $"<<totalAmt<<endl;

cout<<"Actual Amount Paid in 1 Year is : $"<<excessAmt<<endl;

if (excessAmt == 0)

{

cout<<"Congrats !! Your Loan is Paid Successfully No Dues from your Side"<<endl;

}

else

{

cout<<"Warning: You Need to Pay Excess Amount : $ " << excessAmt <<" so The Loan cannot be repaid and can't be closed !!!"<<endl;

}

return 0;

}