• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Trouble with loan calculator program

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to give your output numbers more space. I.e
 
Marshal
Posts: 73981
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Adam Long
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the suggestions. I appreciate the help. I got it pretty much figured out.
     
    Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic