Results 1 to 7 of 7
Thread: Years to Double
 10102011, 05:28 AM #1Member
 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
/////////////////////////////////////////////////////////////////////////
Thanks in advance!
 10102011, 05:49 AM #2Member
 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?
 10102011, 05:56 AM #3Member
 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
 10102011, 06:14 AM #4Member
 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;
}
}
 10102011, 06:53 AM #5Member
 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;
}
 10102011, 07:20 AM #6Member
 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.
 10102011, 08:03 AM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,039
 Blog Entries
 7
 Rep Power
 23
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,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
Similar Threads

How many years have you been using Java?
By derrickD in forum Jobs DiscussionReplies: 41Last Post: 05062011, 07:15 AM 
for loop leap years
By tabako in forum New To JavaReplies: 4Last Post: 10142010, 02:22 AM 
Java/JEE developer with more than 6 Years experience
By nusolid in forum Jobs WantedReplies: 3Last Post: 07222010, 02:21 PM 
Java is 15 years old  what is the Future?
By mikeVB in forum Jobs DiscussionReplies: 1Last Post: 03042010, 05:21 AM
Bookmarks