1. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

## 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
/////////////////////////////////////////////////////////////////////////

2. Member
Join Date
May 2011
Posts
35
Rep Power
0

## Re: Years to Double

yearsToDoubl e?

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

3. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

## 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. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

## 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. Member
Join Date
May 2011
Posts
35
Rep Power
0

## 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. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

## 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. ## 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

#### Posting Permissions

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