programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Loan payment calculator

Thanh Hoang
Greenhorn
Posts: 6
This is my code. I don't know what I should put in the while loop and I'm stuck at the second method, which is the pay off loan.

Knute Snortum
Sheriff
Posts: 4276
127

Do you mean the do/while loop?  Right now it loops forever; probably not a good idea.  How about asking if they user wants to calculate another loan and in the while, test for yes/no?

What are you stuck on?  Please TellTheDetails (that's a link).

Campbell Ritchie
Marshal
Posts: 56536
172
...and welcome to the Ranch

Thanh Hoang
Greenhorn
Posts: 6
Yes. I want my program 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, my program should notify the user what the minimum monthly payment is

Knute Snortum
Sheriff
Posts: 4276
127
So you weren't given these formulae?  Does this help?

https://en.wikipedia.org/wiki/Amortization_calculator

Thanh Hoang
Greenhorn
Posts: 6
I'm sorry but no, It doesn't help anyway. I'm stuck on the formular to calculate the interest month and count how many month did we use to paid off.
Like this example
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

Knute Snortum
Sheriff
Posts: 4276
127
Does this help?

https://en.wikipedia.org/wiki/Amortization_schedule#Outstanding_loan_balance_calculation

(I'm just Googling this stuff.  The keyword is "amortization".  So this link was found with amortization number of payments)

Thanh Hoang
Greenhorn
Posts: 6
This is my code so far. And I still don't understand about the interest month. If you can give me a hint or a code, It will be my pleasure.

Knute Snortum
Sheriff
Posts: 4276
127

Knute Snortum
Sheriff
Posts: 4276
127
I still don't understand about the interest month. If you can give me a hint or a code...

I believe I've given you the information you need.  If you have a specific question, I'll be glad to answer it.  If you're looking for me or anyone to supply you with the code, that's not going to happen.  CodeRanch is not a code mill.

Piet Souris
Master Rancher
Posts: 2044
75
I tried to give a hopefully comprehensible explanation regarding some elementary financial maths formulae.

Let the monthly payment be P, and let the monthly interest be rr (per unit loan, for instance 0.005). Since we do not want the unamortized part of the loan to increase each month, we need to make P at least equal to the monthly interet payment. So we get, the loan being L:

1) P >= rr * L (if P = rr * L, we call this a perpetuity,  a never ending loan)

2) let the unamortized Loan at the end of month m, right after the monthly payment, be UL(m). We get this recursion relation (can you think of why?)

UL(m + 1) = UL(m) * (1 + rr) - P

3) and of course, UL(0) = L

Now, at the end of each month m, just before the payment P, the UL is equal to UL(m) * (1 + rr). To determine both the duration and the final payment, we get this while loop:

Note: I typed this code straight from  the keyboard, and so is untested.

HTH,
Piet

edit: reversed the lines of interestTotl and UL

Thanh Hoang
Greenhorn
Posts: 6
It will run to infinity This is my code so far.

Can we count the number on the new balance as the number of month?

Piet Souris
Master Rancher
Posts: 2044
75
Thanh Hoang wrote:It will run to infinity

Not when I run my code.
Thanh Hoang wrote:Can we count the number on the new balance as the number of month?

Well, test it, but what 'number on the new balance' are you referrng to?

Anyway, I ran your latest code, together with my code. This is what I got:

** Welcome to the Consumer Loan Calculator **
How much do you want to borrow? \$100
What is the annual interst rate expressed as a percent? 12
What is the monthly payment amount? \$4
Your dept will be paid off after months97.0
with a final payment of just \$101.0
The total amount of interest you
will pay during that time is \$1.0
************************************
loan is amortized after 29 months.
with a final payment of  3,65
total interest paid = 15,65
*******************************************

So there is still some work to be done. See if you can spot why you got an infinite run time with my code, it means giving the code some thorough testing!

Campbell Ritchie
Marshal
Posts: 56536
172
Knute Snortum wrote:Please surround your code with code tags.  . . .
I corrected that bit, and doesn't it look better Your code tags shou‍ld look like this:-
[code=java]
if (tagsOK)
{
System.out.println("Code tags OK");
}
[/code]

...and welcome to the Ranch

 Consider Paul's rocket mass heater.