Jump to content

Need help writing PHP script for a school project!


canadian_angel

Recommended Posts

Hi I have to do a project for school that requires me to write PHP script that calculates a 15% return on an investment of $10,000. I have to Calculate the number of years required for a single $10,000 investment to reach $1,000,000 at an average annual return of $15%. I need to use a  looping statement and assume that each loop is equivalent to one year. And wow I really don't know where to begin. I am new at PHP and any help at all would be so much appreciated.

Thanks!

Well I shouldn't really tell you the answer, but I think that the code will explain itself. I think I've done the right thing by the way (add 15% a year).

 

<?php

$investment = 10000; // Set investment amount.
$i = 0; // Set years to 0.

while($investment < 1000000) {
    $investment = $investment * 1.15; // Add 15%.
    $i++; // Add 1 year.
}

echo "Number of years: ".$i;

?>

for ($investment = 10000, $return = 1.15, $years = 1, $goal = 1000000;
     $investment <= $goal;
     $investment *= $return, ++$years) {
  echo 'Investment: ', $investment, ' Return: ', ($investment * $return), ' Year: ', $years, "<br>\n";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.