- Here's the solution, or one solution,
to the challenge 17-01, which is
the monthly payment on a loan.
First, in the main, we are going
to ask the user the questions needed
to be answered in order to calculate
the loan, which is loan amount,
yearly interest rate, number of years.
Those are the only three things that are needed.
Those are variables defined in lines 20 through 22.
Then we asked the user to enter their values.
Then we are calling the function called
loan payment, passing the amount,
number of years, and rate.
Let's take a look at the loan payment,
which implements the forumla I
gave you in the challenge.
First, we're calculating the interest, which is
the rate divided by 100, divided by 12, so if the
yearly rate was 6, we divide 6 by 100 and by 12.
N is the number of years times 12,
because there are 12 payments in a year.
Then Pow is the power function,
meaning 1 plus interest to the power n.
I named those two variables top
and bottom because if you're recalling
the formula in the challenge,
there was a very large fraction.
I decided to use the numerator and
denominator and call them top and bottom.
This is what those formulas are.
Then D, the discount, is the top divided
by the bottom and the payment is the
amount divided by the discount.
Here is the payment.
Let's run this and see how this works.
Loan amount, let's say the loan
amount is $100,000.
Let's say the interest rate is 6% a year.
Let's say the number of years is 30.
Then the monthly payment is $599.
The total amount is $215,838.
We can run it in another scenario.
Let's say would somebody buy a typical
car, let's say a $20,000 loan on a car.
The interest rate is low, let's say 2%.
We're going to finance that over 4 years.
The monthly payment on a $20,000
car for 4 years, 2%, it's $433 a month.
You will be paying back a total of $20,827.