Jump to content

Interest Calc and print


hanes

Recommended Posts

I am trying to calculate the interest rate earned for every year up until retirement age (65) including the interest rate the user inputs.

 

ex: 

Current_age = 30

Interest rate = 6.0

Contribute_amout = 500

Calculates it annually or monthly

 

I want to display the Total amount earned for every year up until retirement age

ex: 

year 1 earned $530.00

year 2 earned $1091.80

year 3 earned $1187.31

etc...
Link to comment
Share on other sites

 

try

 

$age = 30;
$capital = 500;
$rate = 0.06;

echo '<pre>';
printf('%3s | %10s | %10s<br>', 'Age', 'interest', 'capital');
for ($y=$age+1; $y<=65; $y++) {
    $interest = $capital*$rate;
    $capital += $interest;
    printf('%3d | %10.2f | %10.2f<br>', $y, $interest, $capital);
}

interest is seems not to be calculating the right initial amount. The result should look something like this. I cant seem to figure out the right formula.

 

 

Year Contribution Amount Interest Earned Account Total

1   $500.00   $30.00     $530.00

2   $500.00   $61.80     $1,091.80

3   $500.00   $95.51     $1,687.31

4   $500.00   $131.24   $2,318.55

5   $500.00   $169.11   $2,987.66

Edited by hanes
Link to comment
Share on other sites

Looks like you want to add the same amount each year. A trivial change to Barand's program:

$age = 30;
$contribution = 500;
$rate = 0.06;

echo '<pre>';
printf('%3s | %10s | %10s<br>', 'Age', 'interest', 'capital');
for ($y=$age+1; $y<=65; $y++) {
    $capital += $contribution;
    $interest = $capital*$rate;
    $capital += $interest;
    printf('%3d | %10.2f | %10.2f<br>', $y, $interest, $capital);
}

produces the following output:

Age |   interest |    capital
 31 |      30.00 |     530.00
 32 |      61.80 |    1091.80
 33 |      95.51 |    1687.31
 34 |     131.24 |    2318.55
 35 |     169.11 |    2987.66
...
Link to comment
Share on other sites

The result should look something like this. I cant seem to figure out the right formula.

 

 

Year Contribution Amount Interest Earned Account Total

1   $500.00   $30.00     $530.00

2   $500.00   $61.80     $1,091.80

3   $500.00   $95.51     $1,687.31

4   $500.00   $131.24   $2,318.55

5   $500.00   $169.11   $2,987.66

 

I was showing annual interest, yours is cumulative interest. Try

 

$age = 30;
$capital = 500;
$rate = 0.06;
$cumulative = 0;
echo '<pre>';
printf('%3s | %10s | %10s | %10s<br>', 'Age', 'interest', 'Accum', 'capital');
for ($y=$age+1; $y<=65; $y++) {
    $interest = $capital*$rate;
    $cumulative += $interest;
    $capital += $interest;
    printf('%3d | %10.2f | %10.2f | %10.2f<br>', $y, $interest, $cumulative,$capital);
}
Link to comment
Share on other sites

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.