Objective To calculate how long it will take to pay off a loan using c++ Program
ID: 3694782 • Letter: O
Question
Objective To calculate how long it will take to pay off a loan using c++
Program will ask the user to enter an amount of money they want to borrow, the interest rate, and the monthly payment amount. Your program will then determine how many months it will take to pay off that loan, what the final payment amount will be, and how much interest was paid over that time. If the monthly payment amount isn't high enough to pay off more than one month's interest, your program should notify the user what the minimum monthly payment is.
Here are some examples of how your program should behave. Please use the exact same test cases as these, so that I can verify that your program works:
$ a.out
** Welcome to the Consumer Loan Calculator **
How much do you want to borrow? $1000
What is the annual interest rate expressed as a percent? 18
What is the monthly payment amount? $50
Your debt will be paid off after 24 months, with a final payment of just $47.83
The total amount of interest you will pay during that time is $197.83
** Don't get overwhelmed with debt! **
$ a.out
** Welcome to the Consumer Loan Calculator **
How much do you want to borrow? $15000
What is the annual interest rate expressed as a percent? 10
What is the monthly payment amount? $100
You must make payments of at least $126
Because your monthly interest is $125.00
** Don't get overwhelmed with debt! **
$ a.out
** Welcome to the Consumer Loan Calculator **
How much do you want to borrow? $-50
You must enter a positive number!
How much do you want to borrow? $-200
You must enter a positive number!
How much do you want to borrow? $20000
What is the annual interest rate expressed as a percent? -2.5
You must enter a positive number!
What is the annual interest rate expressed as a percent? 5
What is the monthly payment amount? $0
You must enter a positive number!
What is the monthly payment amount? $200
Your debt will be paid off after 130 months, with a final payment of just $125.79
The total amount of interest you will pay during that time is $5925.79
** Don't get overwhelmed with debt! **
Requirements and Hints
The user enters the interest rate as an annual percentage rate. You need to convert this annual percentage to a monthly decimal. For example, 12% annual interest is 1% per month, which is 0.01
Your program must allow only positive numbers to be entered by the user, as shown above
This program should mimic the standard way loans work for credit cards, car loans, and home mortgages: Each month the following happens:
The balance (amount owed) is multiplied times the monthly interest rate to determine how much interest is owed this month. This amount of interest is added to the principal (additional debt)
This amount of interest is also added to the total interest so far (to be output at end)
The monthly payment is subtracted from the balance, reducing the amount of debt.
The month is counted (to be output at end)
This is repeated each month until the debt is paid off (balance reaches zero). The best way to understand it is to look at an example. Let's say you borrow $100. That's also called the balance or principal. Imagine your interest rate is 12%. That's an annual (yearly) rate, so it's 1% per month, so you multiply the balance times 0.01 to get the amount of interest. If your monthly payment is $50/month, then here's what happens:
Month 1:
$100 * 0.01 = $1 interest for the month
adding interest and subtracting the payment:
$100 + $1 - $50 = $51 new balance
Month 2:
$51 * 0.01 = $0.51 interest for the month
adding interest and subtracting the payment:
$51 + $0.51 - $50 = $1.51 new balance
Month 3:
$1.51 * 0.01 = $0.02 interest for the month (you don't have to round in your calculations)
adding interest and subtracting the payment:
$1.51 + $0.02 - $50 = -$48.47 new balance
Since the new balance is negative, the loan is paid off, and the final payment is too much. It should have been $50 - $48.47 = $1.53 final payment.
Your program must notify the user if their monthly payment isn't high enough, and tell them what the minimum payment is to pay off one month's interest (and thus make progress paying off the debt).
Your program must use at least 2 functions in addition to main. At least one of these functions must use a call-by-reference parameter. These functions can be of your choosing, but here are two ideas for functions you could write for your program:
void get_input(double& principal, double& interest, double& payment);
/* This function asks the user to input the principal, interest,
and payment amount, and only accepts positive numbers for input.
All three parameters are call-by-reference, so the function
in effect "returns" the three values by changing the values of
the arguments which are passed to it. The initial values of
these parameters are ignored and overwritten.
*/
double pay_off_loan(double principal, double rate, double payment,
int& months, double& total_interest);
/* This function assumes the payment is enough
to cover first month's interest. It has a loop to
pay off the loan one month at a time.
Pre-Condition:
principal = starting principal.
rate = monthly interest rate expressed as a decimal.
payment = amount of monthly payment, which must be high
enough to pay off the first month's interest.
Post-Condition:
amount of final payment is returned by the function.
months = number of months it took to pay off loan.
total_interest = total amount of interest paid during
that time.
It may help to think of principal, rate, and payment as
input parameters, and months, and total_interest
as output parameters.
*/
Start small! Once you've written your first function, write some code in main() to test it. Compile your program and make sure it works the way you expect it to. Once you have the first function working, add in the second function, or parts of it, and compile/test again.
Don't use any global variables. Every variable should be declared inside of a function.
Put comments at the top of each function indicating what the function does. If the function has parameters and/or returns a value, provide a comment describing each.
Save a text file with your program's source (C++) code and the output it produced after you compiled and ran it. The file should be in plain text format, with a file extension of either .txt or .cpp
Explanation / Answer
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// asks user how much they want to borrow, the interest, and how much they
//are going to pay each month
void get_input(double& principle, double& interest, double& payment);
//tells user how long it will take to pay off the loan and the total
interest
//they will pay
double pay_off_loan(double principle, double rate, double payment, int&
months, double& total_interest);
//checks if a negative number was entered or not
bool negative_check(float number);
int main()
{
double principle, interest, payment, total_interest = 0;
int months = 0;
cout << " ** Welcome to the Consumer Loan Calculator ** ";
get_input(principle, interest, payment);
// if they can pay if off within a month, let them know
if (payment >= principle)
{
cout << "If you pay this off within a month - no
interest"; cout << " for you! If not,";
}
pay_off_loan(principle, interest, payment, months,
total_interest);
cout << " ** Don't get overwhelmed with debt! ** ";
return (0);
}
void get_input(double& principle, double& interest, double& payment)
{
// ask them how much they want to borrow, repeats if 0 or below
do{
cout << "How much do you want to borrow? $";
cin >> principle;
}while(negative_check(principle));
// ask what is interest rate, repeats if 0 or below
do{
cout << "What is the annual interest rate expressed ";
cout << "as a percent? ";
cin >> interest;
}while(negative_check (interest));
// ask how much they will pay each month, repeats if 0 or below
do {cout << "What is the monthly payment amount? $" ;
cin >> payment;
}while(negative_check (payment));
interest = interest / 12 / 100;
}
double pay_off_loan(double principle, double rate, double payment, int&
months, double& total_interest)
{
double min_pay, last_payment;
//calculates how many months needed to pay off, and total interest
paid
while (principle > 0)
{
min_pay = principle * rate;
//if they are not paying enough to cover the interest,
tells //them the minimum amount they must pay to
eventually pay off //loan
if (payment < min_pay)
{
cout << "You have to pay at least $" << fixed <<
setprecision(2) << min_pay + 1;
cout << ". Because your monthly interest is $" <<
fixed << setprecision(2) << min_pay << ".";
return (0);
}
//calculates the month and interest if not underpaying or
//overpaying
else
{
min_pay = principle * rate;
principle = principle + min_pay - payment;
months++;
total_interest = min_pay + total_interest;
}
}
last_payment = principle + payment;
cout << " Your debt will be paid off after " << months;
cout << " month(s), with a final payment of just $" << fixed <<
setprecision(2) << (last_payment) << endl;
cout << "The total amount of interest you will pay during that";
cout << " time is $" << fixed << setprecision(2) <<
(total_interest);
}
// checks if the number inputted is negative or not
bool negative_check (float number)
{
if (number <= 0)
{
cout << "You must enter a positive number! ";
return (number <= 0);
}
else return false;
}
sample output
** Welcome to the Consumer Loan Calculator **
How much do you want to borrow? $1000
What is the annual interest rate expressed as a percent? 18
What is the monthly payment amount? $50
Your debt will be paid off after 24 months, with a final payment of just
$47.83
The total amount of interest you will pay during that time is $197.83
** Don't get overwhelmed with debt! **
** Welcome to the Consumer Loan Calculator **
How much do you want to borrow? $15000
What is the annual interest rate expressed as a percent? 10
What is the monthly payment amount? $100
You have to pay at least $126.00.
Because your monthly interest is $125.00.
** Don't get overwhelmed with debt! **
mimi@Toshiba:~/Desktop/CS110A$ ./a.out
** Welcome to the Consumer Loan Calculator **
How much do you want to borrow? $-50
You must enter a positive number!
How much do you want to borrow? $-200
You must enter a positive number!
How much do you want to borrow? $20000
What is the annual interest rate expressed as a percent? -2.5
You must enter a positive number!
What is the annual interest rate expressed as a percent? 5
What is the monthly payment amount? $0
You must enter a positive number!
What is the monthly payment amount? $200
Your debt will be paid off after 130 months, with a final payment of just
$125.79
The total amount of interest you will pay during that time is $5925.79
** Don't get overwhelmed with debt! **
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.