Jump to content

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";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.