Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: Years to Double

  1. #1
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Post Years to Double

    Hey everyone,

    I have a code here that has to check how long it takes for a value to double at any given interest rate. This is what i have so far, however it won't print anything except for rate(percent and years to double

    Any ideas on what i'm doing wrong


    /////////////////////////////////////////////////////////////////////////
    class Hw05
    {
    //-----------------------------------------------------------------------
    public static void main ( String [] args ) throws Exception
    {
    System.out.println("rate(percent) years to double");
    for ( int percent : new int[] {1,2,3,4,6,9,12,24} ) line(percent);
    }
    //-----------------------------------------------------------------------
    public static void line ( int percent )
    {
    System.out.printf("%7d%17d\n",percent,yearsToDoubl e(percent));
    }
    //-----------------------------------------------------------------------
    public static int yearsToDouble ( int percent )
    {
    int years = 0;
    int initialAmount = 100;
    while (initialAmount <= 200)
    {
    initialAmount *= percent; //compound interest
    years++;
    }
    return years;
    }
    }
    //-----------------------------------------------------------------------
    // end class Hw05
    /////////////////////////////////////////////////////////////////////////


    Thanks in advance!

  2. #2
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default Re: Years to Double

    yearsToDoubl e?

    Is the code above that you posted have any syntax error?

  3. #3
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Years to Double

    Hmm don't know why it did that on here. On my code there is no space. no syntax errors.

    do i need to multiply percent by .01 and then do

    newAmount = ((intererest rate in percent form) * initial amount) + intial amount

  4. #4
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Years to Double

    Here is what i got now and it returns all 2's for years to double

    {
    //-----------------------------------------------------------------------
    public static void main ( String [] args ) throws Exception
    {
    System.out.println("rate(percent) years to double");
    for ( int percent : new int[] {1,2,3,4,6,9,12,24} ) line(percent);
    }
    //-----------------------------------------------------------------------
    public static void line ( int percent )
    {
    System.out.printf("%7d%17d\n",percent,yearsToDoubl e(percent));
    }
    //-----------------------------------------------------------------------
    public static int yearsToDouble ( int percent )
    {
    int yearsToDouble = 0;
    int initialAmount = 100;
    double total= initialAmount * .01;
    while (total <= 200)
    {
    total = (total *initialAmount) + initialAmount; //compound interest
    yearsToDouble++;
    }
    return yearsToDouble;
    }
    }

  5. #5
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default Re: Years to Double

    Try this

    public static int yearsToDouble(int percent) {
    int yearsToDouble = 0;
    double total = 100;
    while (total <= 200) {
    total = total * (100+percent)/100;
    yearsToDouble++;
    }

    return yearsToDouble;
    }

  6. #6
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Years to Double

    Wow you are a life saver! Thank you so much for your help.

    could you explain what you did with the total = total * (100+percent)/100; ?

    again thank you so much.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,533
    Blog Entries
    7
    Rep Power
    20

    Default Re: Years to Double

    Oh dear, oh dear, this sure is a forum full of mathematically challenged folks ... where's my blackboard ...
    If you have an interest percentage i then the multiplication rate r is 1+i/100 or (100+i)/100 (check this), so if you have an amount of money p, after one year you have an amount of money r*p, after two years you have r*r*p and after n years you have r**n*p (where ** is the power operator).

    The question is, when have you doubled your present amount of money p? So:

    2*p == r**n*p where n is the unknown value; continuing:

    log(2*p) == log(r**n*p) -->

    log(2*p) == n*log(r)+log(p) -->

    log(2)+log(p) == n*log(r)+log(p) -->

    log(2) == n*log(r) -->

    n == log(2)/log(r)

    As you can see there is no mention of the present amount p in the equation, i.e. every amount of money doubles in log(2)/log(r) years. There is no need for loops whatsoever; the above equation is enough.

    kind regards,

    Jos
    Iron Lion likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How many years have you been using Java?
    By derrickD in forum Jobs Discussion
    Replies: 41
    Last Post: 05-06-2011, 07:15 AM
  2. for loop leap years
    By tabako in forum New To Java
    Replies: 4
    Last Post: 10-14-2010, 02:22 AM
  3. Replies: 3
    Last Post: 07-22-2010, 02:21 PM
  4. Java is 15 years old - what is the Future?
    By mikeVB in forum Jobs Discussion
    Replies: 1
    Last Post: 03-04-2010, 04:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •