I'm working on using a for-loop to create a table of values for a loan with payments over the course of a year. My code compiles and runs, and my output is fine, but I cannot get me numbers right in my output table.
I'm doing a $5,000 loan @ 12% APR. I think I should set my variables to their initial values before the loop, and then manipulate them mathematically within the loop.
Am I on the right track here?
Here is the code I have so far:
// Loan Calculator @Author Adam Long // calculates a $5,000 loan at 12% APR
I didn't think the algorithm to calculate interest looked correct; when I tried it the interest in the 12th month was $1024000! Were you told to use any particular formula, or to find one. There is a standard amortisation formula; see this Wikipedia article. When it says "interest divided by 100" it means that you write 0.08 rather than 8%. If "0.01" means that you are charging 1% per period on the output, then you ought to have $50 interest 1st time and slightly less next time.
If you are using a fixed payment and calculating the interest without amortising, try this sort of algorithm:
Interest = loan * interest rate
loan = loan - (repayment - interest)
Repeat until you reach 12.
As long as your repayment is more than the initial interest, your loan should gradually reduce. But maybe not to zero in the space of your output. You seem to be calculating values (eg interest before the "for" loop) which you aren't using, and you have multiplied interestRate by loan twice.
I would suggest you get yourself a nice simple algorithm. You are probably being marked for how well you program it, not whether you calculate the repayments correctly.
Please use code tags round any quoted code; it makes it easier to read.