Answer:
Check the explanation
Explanation:
CODE:
#include <iostream>
#include <iomanip>
using namespace std;
double getLoanAmount() { // functions as per asked in question
double loan;
while(true){ // while loop is used to re prompt
cout << "Enter the car loan amount ($2,500-7,500): ";
cin >> loan;
if (loan>=2500 && loan <= 7500){ // if the condition is fulfilled then
break; // break statement is use to come out of while loop
}
else{ // else error message is printed
cout << "Error: $"<< loan << " is an invalid loan amount." << endl;
}
}
return loan;
}
double getMonthlyPayment(){ // functions as per asked in question
double monthpay;
while(true){ // while loop is used to re prompt
cout << "Enter the monthly payment ($50-750): ";
cin >> monthpay;
if (monthpay>=50 && monthpay <= 750){ // if the condition is fulfilled then
break; // break statement is use to come out of while loop
}
else{ // else error message is printed
cout << "Error: $"<< monthpay << " is an invalid monthly payment." << endl;
}
}
return monthpay;
}
double getInterestRate(){ // functions as per asked in question
double rate;
while(true){ // while loop is used to re prompt
cout << "Enter the annual interest rate (1-6%): ";
cin >> rate;
if (rate>=1 && rate <= 6){ // if the condition is fulfilled then
break; // break statement is use to come out of while loop
}
else{ // else error message is printed
cout << "Error: "<< rate << "% is an invalid annual interest rate." << endl;
}
}
return rate;
}
int main() {
cout << setprecision(2) << fixed; // to print with 2 decimal places
int month = 0; // initializing month
// calling functions and storing the returned value
double balance= getLoanAmount();
double payment= getMonthlyPayment();
double rate= getInterestRate();
rate = rate /12 /100; // as per question
// printing as per required in question
cout << "Month Balance($) Payment($) Interest($) Principal($)"<< endl;
while(balance>0){ // while the balance is more than zero
month = month + 1; // counting Months
// calculations as per questions
double interest = balance * rate;
double principal = payment - interest;
balance = balance - principal;
// printing required info with proper spacing
cout <<" "<< month;
cout <<" "<< balance;
cout <<" "<< payment;
cout <<" "<< interest;
cout <<" "<< principal << endl;
}
cout << "Months to repay loan: " << month << endl; // displaying month
cout << "End of Banker Bisons";
Kindly check the output in the attached image below.