Forums Register Login

Trouble with loan calculator program

+Pie Number of slices to send: Send
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


public class Asgn4a {

public static void main( String[] args ) {

System.out.println( "Payment Interest Principal Balance Total Int ");
System.out.println( "=================================================");

double loan = 5000.00;
double interestRate = .01;
double interest = loan * interestRate;
double principal = (loan * 1.01) - interest;
double payment = principal + (interestRate * loan);
double balance = loan - principal;

for ( int count = 1; count <= 12; count++ ) {

interest = balance * interestRate;

principal = loan - balance;

balance -= principal;

double totalInt = interest * count;

System.out.printf( "%3d %9.2f %9.2f %9.2f %9.2f %9.2f\n",
count, interest, principal, balance, totalInt, payment);

}
}
}
[ October 21, 2007: Message edited by: Adam Long ]
+Pie Number of slices to send: Send
You need to give your output numbers more space. I.e
+Pie Number of slices to send: Send
Welcome to the Ranch.

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.
    +Pie Number of slices to send: Send
    Thanks for the suggestions. I appreciate the help. I got it pretty much figured out.
    My pie came with a little toothpic holding up this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1315 times.
    Similar Threads
    Help on Control Statement
    Java Course help needed -Mortgage
    cant get program loop to work?
    Mortgage Calculator and GUI
    formatting decimal spaces
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 12:33:51.