Write a program that asks the user for a loan amount an annu
Write a program that asks the user for a loan amount, an annual interest rate, and a monthly payment amount; call a function called payoff which displays a list of the account month by month and then returns the number of months needed to payoff the loan. For example:
Please enter loan amount: 1000.00 Please enter monthly payment: 100.00 Please enter annual interest: 5.00 Interest rate per month is 0.42 Mon: 1 Bal: 1000.00 + Int:4.17 - Pay:100.00 Mon: 2 Bal: 904.17 + Int:3.77 - Pay:100.00 Mon: 3 Bal: 807.93 + Int:3.37 - Pay:100.00 Mon: 4 Bal: 711.30 + Int:2.96 - Pay:100.00 Mon: 5 Bal: 614.26 + Int:2.56 - Pay:100.00 Mon: 6 Bal: 516.82 + Int:2.15 - Pay:100.00 Mon: 7 Bal: 418.98 + Int:1.75 - Pay:100.00 Mon: 8 Bal: 320.72 + Int:1.34 - Pay:100.00 Mon: 9 Bal: 222.06 + Int:0.93 - Pay:100.00 Mon: 10 Bal: 122.98 + Int:0.51 - Pay:100.00 Last payment: 23.50 It will take 11 months to pay off the loan.
Also, make sure the loan does not take more than 360 payments (30 years + a payoff). For example, if a loan will cost more in interest than the payments, it will never end, so we need to trap for that:
Please enter loan amount: 50000.00 Please enter monthly payment: 400.00 Please enter annual interest: 10.00 Interest rate per month is 0.83 Mon: 1 Bal: 50000.00 + Int:416.67 - Pay:400.00 Mon: 2 Bal: 50016.67 + Int:416.81 - Pay:400.00 Mon: 3 Bal: 50033.47 + Int:416.95 - Pay:400.00 Mon: 4 Bal: 50050.42 + Int:417.09 - Pay:400.00 Mon: 5 Bal: 50067.50 + Int:417.23 - Pay:400.00 Mon: 6 Bal: 50084.73 + Int:417.37 - Pay:400.00 … Mon: 357 Bal: 86379.40 + Int:719.83 - Pay:400.00 Mon: 358 Bal: 86699.23 + Int:722.49 - Pay:400.00 Mon: 359 Bal: 87021.73 + Int:725.18 - Pay:400.00 Mon: 360 Bal: 87346.91 + Int:727.89 - Pay:400.00 Report was stopped, does not seem to end.
MORE
Start with this code:
#include <iostream> // YOUR CODE HERE
// TO HERE!
// DO NOT CHANGE ANYTHING BELOW int main() {
double loan, payment, interest;
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
cout << \"Please enter loan amount: \";
cin >> loan; cout << loan << endl;
cout << \"Please enter monthly payment: \";
cin >> payment; cout << payment << endl;
cout << \"Please enter annual interest: \";
cin >> interest; cout << interest << endl;
int numMonths = payoff(loan, payment, interest);
if (numMonths != -1) {
cout << \"It will take \" << numMonths;
cout << \" months to pay off the loan.\" << endl;
}
return 0;
}
Solution
//Programme to find months to pay loan amount
#include <iostream> // input output file
#include <iomanip> // std::setprecision
// DO NOT CHANGE ANYTHING BELOW
using namespace std;
// function declaration
int payoff(double l,double p,double i);
int main()
{
double loan, payment, interest;
cout.setf(ios::fixed, ios::floatfield); //configure precision
cout.precision(2); //set to precision 2;
cout << \"Please enter loan amount: \";
cin >> loan; //read loan amount
cout << loan << endl;
cout << \"Please enter monthly payment: \";
cin >> payment; //read monthly paymnet
cout << payment << endl;
cout << \"Please enter annual interest: \";
cin >> interest; //read annuval rate
cout << interest << endl;
cout <<\"Interest rate per month is\" << (interest/12)<<endl;;
int numMonths = payoff(loan, payment, interest); //function call to payoff
if (numMonths != -1) //if numMonts is equal to -1 report error otherwise display numMonths
{
cout << \"It will take \" << numMonths;
cout << \" months to pay off the loan.\" << endl;
}
else
cout << \" Report was stopped, does not seem to end.\" << endl;
return 0;
}
int payoff(double l,double p,double i)
{
int x;
float sum=0,nop=0,tsum=0;
for ( x=0;l>p;x++) //loop for finding number months to monthly installments
{
if(nop<=360) //check months crosses to 360
{
sum=(l*(i/12)*1)/100; //loan * interest per month * one month
tsum=sum+l; //tsum with principal amount with interest
cout << \"Mon: \"<<x+1<<\" Bal:\" <<l <<\" + Int:\"<<sum << \"- Pay: \"<<p <<endl; //dispaly output in required format
l=tsum-p; //laon amount reduced after monthly paymnet;
}
else
{
return -1;
}
nop++;
}
if(l>0) //last month remaing amount grater than zero
{
cout<< \"Last payment: \" << l <<endl ;
x=x+1;
return x;
}
else
{
return x;
}
}
Output :
Please enter loan amount: 1000
1000.00
Please enter monthly payment: 100
100.00
Please enter annual interest: 5
5.00
Interest rate per month is0.42
Mon: 1 Bal:1000.00 + Int:4.17- Pay: 100.00
Mon: 2 Bal:904.17 + Int:3.77- Pay: 100.00
Mon: 3 Bal:807.93 + Int:3.37- Pay: 100.00
Mon: 4 Bal:711.30 + Int:2.96- Pay: 100.00
Mon: 5 Bal:614.26 + Int:2.56- Pay: 100.00
Mon: 6 Bal:516.82 + Int:2.15- Pay: 100.00
Mon: 7 Bal:418.98 + Int:1.75- Pay: 100.00
Mon: 8 Bal:320.72 + Int:1.34- Pay: 100.00
Mon: 9 Bal:222.06 + Int:0.93- Pay: 100.00
Mon: 10 Bal:122.98 + Int:0.51- Pay: 100.00
Last payment: 23.50
It will take 11 months to pay off the loan.


