Page 1 of 1

compound interest to $1,000,000.

Posted: Fri Jun 11, 2010 1:31 pm
by cjkeane
Hi.

I am trying to figure out how much compound interest i will generate if i have 15% interest per year on an initial $10,000 investment. The script runs fine and generates the interest correctly, however, i am having a problem getting it to stop at $1,000,000. Any help would be appreciated.

thx.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Investment Calculator</title>
    </head>
    <body>
    
<div align="center"><br/>
<table border="0" cellspacing="0" cellpadding="0" width="340">
<?php
$years = 50'; // just a placeholder so years can be established.
$percent = '.15';
$amount = '10000';
$goal = '1000000';

if ($percent > 1.0) $percent /= 100.0;
$p = 1;
for ($i = 0; $i < $years; $i++)
$result[$i] = $amount * ($p *= (1 + $percent));

print "<tr><td><b>Year</b></td><td><b>Amount</b></td></tr>\n";
foreach ($result as $year => $amt) print "<tr><td>".($year+1)."</td><td>$".number_format($amt,2)."</td/</tr>\n";

?>
</table>
</div>

    </body>
</html>

Re: compound interest to $1,000,000.

Posted: Fri Jun 11, 2010 2:52 pm
by lunarnet76
you can simply use

Code: Select all

for ($i = 0; $i < $years; $i++){
            $result[$i] = $amount * ($p *= (1 + $percent));
if($result[$i]>$goal)break;
}

Re: compound interest to $1,000,000.

Posted: Fri Jun 11, 2010 3:40 pm
by cjkeane
thanks.. i knew i needed a break.. but didnt know where to put it...