hanes Posted March 18, 2013 Share Posted March 18, 2013 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 https://forums.phpfreaks.com/topic/275792-interest-calc-and-print/ Share on other sites More sharing options...
Barand Posted March 18, 2013 Share Posted March 18, 2013 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); } Link to comment https://forums.phpfreaks.com/topic/275792-interest-calc-and-print/#findComment-1419231 Share on other sites More sharing options...
hanes Posted March 18, 2013 Author Share Posted March 18, 2013 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 Link to comment https://forums.phpfreaks.com/topic/275792-interest-calc-and-print/#findComment-1419233 Share on other sites More sharing options...
davidannis Posted March 18, 2013 Share Posted March 18, 2013 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 https://forums.phpfreaks.com/topic/275792-interest-calc-and-print/#findComment-1419238 Share on other sites More sharing options...
hanes Posted March 18, 2013 Author Share Posted March 18, 2013 Perfect, except for the fact that it calculates it annually. What would the difference be if it was monthly? Link to comment https://forums.phpfreaks.com/topic/275792-interest-calc-and-print/#findComment-1419240 Share on other sites More sharing options...
Barand Posted March 18, 2013 Share Posted March 18, 2013 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 https://forums.phpfreaks.com/topic/275792-interest-calc-and-print/#findComment-1419270 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.