Alama
Alama

Reputation: 1

calculating a compound interest in php

The formula for compound interest is

A = P(1 + r/n) ^ nt 

Now, if I invest $60,000 for 1 year at 15%, my interest gained would be $9000.

If I add it to my initial $60,000 the the final amount = $69,000.

Here's my problem: if the year is more than one year, I want to add my initial $60,000 to the final amount of $69000 to get a new value, $129000, which i will multiply with 15% to get a new interest value of $19,350 and add it to the new value, $129,000, to get a new amount = $148350. and this will continue for the number of years specified.

Year 1 = $60000 + $9000 = $69000
Year 2 = $69000 + $60000 = $129000
  ($129000*15%=$19350)
  $129000+$19350 =$148350
Year 3 = $148350 + $60000 = $208350
  ($208350*15%=$31252.50)
  $208350+$31252.50 =$239602.50

... Etc

Upvotes: 0

Views: 14650

Answers (3)

Ali Ahmad Pasa
Ali Ahmad Pasa

Reputation: 156

Find the below upgraded one fixed for multiple years

<?php
// PHP program to find
// compound interest for
// given values.

$years = 10; // number of years
$initial = 10000; // Principal amount
$rate = 10; // Rate of Interest
$n =4; // number of compounding in a year

function CompoundInterest($investment, $year, $rate=10, $n=1)
{
    $accumulated=0;
    if ($year > 1)
    {
        // print_r($investment);
        $accumulated = CompoundInterest($investment, $year-1 , $rate, $n);
    }else {
        $accumulated += $investment;
    }

    $accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
    return $accumulated;
}

$totalMaturity = CompoundInterest($initial,$years,$rate,$n);
echo "Deposit Amount is ". $initial."\n";
echo '<br>';
echo "After ". $years. " years, the accumulated interest is ". $totalMaturity."\n";
echo '<br>';
echo "After ". $years. " years, Interest is ". ($totalMaturity - $initial) ."\n";

?>

Upvotes: 0

onurmutluay
onurmutluay

Reputation: 51

First you get the post values as

<?php
$principal = $_POST['principal'];
$year = $_POST['year'];
$interest = $_POST['interest'];
?>

You may calculate by defining a function by considerin decimals:

<?php

function compound_interest($principal, $year, $interest){
    $converted_interest = $interest / 100;
    $completed_interest = $converted_interest + 1;
    $exponent = pow($completed_interest,$year);
    $final = $principal * $exponent;
    return number_format((float)$final, 2, ',', '.');
}

?>

Lastly, run the function:

<?php $calculate = compound_interest($principal, $year, $interest); echo $calculate; ?>

Upvotes: 2

rgunning
rgunning

Reputation: 568

Easiest way is to create a recursive function, assuming same yearly investment

<?php

function interest($investment,$year,$rate=15,$n=1){
    $accumulated=0;
    if ($year > 1){
            $accumulated=interest($investment,$year-1,$rate,$n);
            }
    $accumulated += $investment;
    $accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
    return $accumulated;
    }

?>

Then to run the function according to form input

<html>
<head><title>Calculate Compound Interest</title></head>
<body><h3>Calculate Compound Interest</h3>

<?php
$initial=0;
$years=0;
$rate=15;
$n=1;

if (isset($_POST['initial'])){$initial=$_POST['initial'];}
if (isset($_POST['years'])){$years=$_POST['years'];}
if (isset($_POST['rate'])){$rate=$_POST['rate'];}
if (isset($_POST['n'])){$n=$_POST['n'];}

if (isset($_POST['initial'])){
    echo "After ". $years. " years, the accumulated interest is ". interest($initial,$years,$rate,$n)."\n";
}
?>

And the form input

<?php echo "<form method=\"post\" action=\""  . $_POST['SELF.PHP'] . "\">"; ?>

<p>Initial amount (contribution), $: <?php echo '<input type="number" name="initial" value='. $initial.' required/>'?> </p>
<p> Annual interest rate : <?php echo '<input type="number" name="rate" value='.$rate.' />' ?> % </p>
<p> Number of compounding periods per year? <?php echo '<input type="number" name="n" value='.$n.' />'?> </p>
<p> How many years? <?php echo '<input type="number" name="years" value='. $years.' min="1" required/>' ?></p>
<p> <input type="submit" value="Generate compound interest table."/> </p>
</form> </body> </html>

EDIT: edited to include example of running function with a form input

Upvotes: 2

Related Questions