Good Evening,

 Can anyone help me with the mathematical formula for investment. This the problem that I have to workout
 Write a program to find future value of monthly investments. Start with an initial investment, make a deposit every 30days, calculate interest on principle compounded daily, display a table showing beginning balance, deposit for the year, interest earned for the year, and ending balance.  The table should display the aforementioned information for a duration of your present age to 65.  Display result in a table format as shown below. 

Future Value Calculation
Initial Investment: $2000
Interest Rate: 12%
Deposit every 30 days: $100
Investment started: 50
Age Beg Bal     Interest      Deposits     Ending Bal
51  2000        324.65        1200         3524.65 
52  3524.65     519.01        1200         5243.66 
53  5243.66     738.14        1200         7181.79 
54  7181.79     985.2         1200         9366.99 
55  9366.99     1263.76       1200         11830.74 
56  11830.74    1577.82       1300         14708.57 
57  14708.57    1944.67       1200         17853.24 
58  17853.24    2345.54       1200         21398.77 
59  21398.77    2797.5        1200         25396.28 
60  25396.28    3307.08       1200         29903.36 
61  29903.36    3881.62       1200         34984.98 
62  34984.98    4529.4        1300         40814.38 
63  40814.38    5272.5        1200         47286.88 
64  47286.88    6097.58       1200         54584.46 
65  54584.46    7027.83       1200         62812.29 
2 Years
Discussion Span
Last Post by poloblue
  <title>Future Value Calculation</title>
  <style type = "text/css">
  table {
    width: 80%;
    border-collapse: collpase;
  td {
    border: 1px inset gray;
    padding: 4px;
  th {
    border: 1px inset brown;
    padding: 4px;
  input { text-align: right; }
  .left { text-align: left; }
  .center { text-align: center; }
  .right { text-align: right; }
  <script type = "text/javascript">
    var principal = 2000;
    var rate = .12;
    var deposit = 100;
    var age = 50;

    document.writeln("<h3>Future Value Calculation</h3>");
    document.writeln("Initial Investment: $"+principal+"<br/>");
    document.writeln("Interest Rate: "+(rate*100)+"%<br/>");
    document.writeln("Deposite every 30 days: $"+deposit+"<br/>");
    document.writeln("Invesment Started: "+age+"<br/>");
    document.writeln("<table>" );
    document.writeln("<th>Begin Bal.</th>");
    document.writeln("<th>End Bal.</th></tr>");
    var EndBal, interest;
    for ( var currAge=age; currAge<=65; currAge++ ) {
      document.writeln("<td class='center'>"+currAge+"</td>");
      document.writeln("<td class='right'>"+principal.toFixed(2)+"</td>");
      // compute new future value
      // in computation, "deposit $100" each month
      EndBal = principal * 1;  // *** NEEDS FIXING!!!
      // now compute the interest per year and display it
      interest = EndBal - principal;  // *** NEEDS FIXING!!!
      // after computed it, can now display using JavaScript
      document.writeln("<td class='right'>"+interest.toFixed(2)+"</td>");
      // deposit every month
      document.writeln("<td class='center'>"+(deposit*12).toFixed(2)+"</td>");
      // end balance
      document.writeln("<td class='right'>"+EndBal.toFixed(2)+"</td>");
      // copy the value to the next year
      principal = EndBal;
    document.writeln( "</table>" );

I'd make a form out of this, with number texboxes for all your variables (initial investment, interest rate etc.) and add a calculate button.

When you press the calculate button, the (on)click event gets then values and starts processing the data, concatenating an inital html string for each iteration of the loop. At the end print the string.


so what you're saying is you don't know....

Not at all. If the OP wants to come back, I'll be glad to help, but I won't do the work for him. So do you have anything to add?

Edited by diafol


no, I was merely commenting because I have the exact problem now. I completed the code and i fired it up and it is structured correctly only the results are crazy wrong. So i wanted to know if you would be willing to help still or if you were merely side-stepping. No offense intended. Would you like to see what I have so far?

Edited by Drac: added more text


here is what I have but it returns screwy numbers. I have tried but i think my errors may be part in logic and part in math. Any corrections would be appreciated.

<title>Future Value Calculation</title>
<style type = "text/css">
table { width: 100% }
th    { text-align : left } 
th span { color: #008; margin-left: 15px; }
<script type = "text/javascript">

window.onload = function()
var EndBal;
var principal = 2000.00;
var rate = 0.12;
var deposit = 1200;
var interest;
var BegBal = principal + deposit;
for ( var age = 16; age >= 1; --age )
// Do calculations
EndBal = ((principal + deposit) * Math.pow(1 + rate, age ));
interest = ((principal + deposit) * Math.pow(1 + rate, age ))-(principal+deposit);

// Insert row
var row = getById("tBody").insertRow(0); //Insert row at index 0
// Create cells for the created row
// Empty cells are set to   to be displayed in the table
row.insertCell(0).innerHTML = age+50; 
row.insertCell(1).innerHTML = EndBal.toFixed(2);
row.insertCell(2).innerHTML = interest.toFixed(2);
row.insertCell(3).innerHTML = deposit;
row.insertCell(4).innerHTML = BegBal.toFixed(2);
BegBal = EndBal;

// Set the header values
getById("spnInitial").innerHTML = principal.toFixed(2);
getById("spnRate").innerHTML = rate.toFixed(2);
getById("spnDeposit").innerHTML = deposit;
getById("spnInvestment").innerHTML = EndBal.toFixed(2);

// Helper function to get element by id
function getById(id)
return document.getElementById(id);
<table border="1">
<tr><th colspan="5">Future Value Calculation</th></tr>
<tr><th colspan="5">Initial Investment:<span id="spnInitial"></span></th></tr>
<tr><th colspan="5">Interest Rate:<span id="spnRate"></span></th></tr>
<tr><th colspan="5">Deposit every 30 days:<span id="spnDeposit"></span></th></tr>
<tr><th colspan="5">Investment started:<span id="spnInvestment"></span></th></tr>
<th>Beg Bal</th>
<th>Ending Bal</th>
<tbody id="tBody">


At poloblue..what?! NO! it is as is. Constants. well, at least this is what I need right now anyway. Any help on the javascript code is what I am looking for.

Did you ever finish the code?

Edited by Drac: ediit


Creating the output rows from input vars (via form) is trivial except for the calculation of the interest and therefore the balance at the end of each year.

If I understand the OP, this requires a daily compound interest calculation.

This is complicated by the fact that payments are made every month. Each calendar month will be of a different length and I'm not sure when the monthly payments are made - at the end or the start of each month. Also leap years will account for an extra day.

SO say we start on 1st January 2015 with $2000. If the deposit ($100) is made on the 1st day interest will be calculated on $2100 etc whereas if deposits are made on any other day, the interest for that particular month will be less.

So we need more info - deposit day, e.g. 4th of every month, start year and month (to calculate when we have leap years to give an extra day's interest on Feb 29)

Compounding daily will produce a little more interest than a simple yearly compounding.

Perhaps I'm over-engineering the solution.

I modify my code and now it doesn't want to work anything. Any help will be appreciate.

<!DOCTYPE html>
<title>Future Value Calculator</title>
<style type = "text/css">
table { width: 100% }
th    { text-align : left } 
th span { color: #008; margin-left: 15px; }

<script type="text/javascript">

function myFunction() {
    var x;
x= prompt("Please enter the age: ");
x= parseInt(x);


function createTable(){
    var a = document.getElementById("tb1").value;
    var b = document.getElementById("tb2").value;

        if(a=="" || b==""){
            alert("Please enter some numeric value");

    var a = 65-x;       // number of years to be calculated
    var b=2000;     //begining balance
    var interest=0;     //accumulated interest
    var d=100;      //monthly deposits 
    var dd=0;       //total accumulated deposits
    var f=0;        //future value
    var r= .12;     //interest rate
    var n= 12;      //number of monthly compounds per year
    var t=1;        //time in years
    x++;            //age
    row=new Array();
        cell=new Array();        
    row_num=parseInt(a);            //obtaining the number of rows of the table
        cell_num=parseInt(5);           //set to static number of columns
        tab=document.createElement('table');    //creating table element
        tab.setAttribute('id','newtable');  //setting attributes to the table
        tbo=document.createElement('tbody');    //creating tables body

        for(c=1;c<row_num;c++){      //creating rows



    f= (d*( ( Math.pow( (1+(r/n)), (n*t) ) -1 )/(r/n) ) ) + (b*( Math.pow( (1+(r/n)), (n*t) ) ));       
    interest= (d*( ( Math.pow( (1+(r/n)), (n*t) ) -1 )/(r/n) ) ) + (b*( Math.pow( (1+(r/n)), (n*t) ) )) - (d*12) - b;
    dd= d*12;


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.