compound interest to $1,000,000.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

compound interest to $1,000,000.

Post 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>
Last edited by Benjamin on Fri Jun 11, 2010 1:56 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
lunarnet76
Forum Commoner
Posts: 67
Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh

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

Post 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;
}
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

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

Post by cjkeane »

thanks.. i knew i needed a break.. but didnt know where to put it...
Post Reply