Nandakumar Edamana
Share on:
@ R t f

Compound Interest Calculator Using JavaScript


Given below is the code to create a simple web app that calculates compound interest. You can paste it into a text editor (e.g.: gEdit or Notepad), save it with a filename that has the extension .html (e.g.: simpleint.html), open it in a web browser, and run it.

<html>
    <head>
	<title>Web App for Compound Interest Calculation</title>

        <script>
            function calculate()
            {
                p = document.getElementById("p").value;
                n = document.getElementById("n").value; // no. of compoundings per year
                t = document.getElementById("t").value; // no. of years
                r = document.getElementById("r").value;
                result = document.getElementById("result");

                // The equation is A = p * [[1 + (r/n)] ^ nt]
                A = (p* Math.pow((1 + (r/(n*100))), (n*t)));

                // toFixed is used for rounding the amount with two decimal places.
                result.innerHTML = "The total amount is " + A.toFixed(2);

                result.innerHTML += "<br> The interest is " + (A.toFixed(2) - p).toFixed(2);
            }
        </script>
        <style>
            div {
                display: table-row;
            }

            label, input {
                display: table-cell;
            }
        </style>
    </head>

    <body>
        <h1>Compound Interest Calculation</h1>

        <div> <label>Amount: </label> <input id="p"> </div>
        <div> <label>Rate (%): </label> <input id="r"> </div>
        <div> <label>No. of Years: </label> <input id="t"> </div>
        <div> <label>Compunding Times Per Year: </label> <input id="n" value="1"> </div>

        <button onclick="calculate()">Calculate</button>

        <p id="result"></p>
    </body>
</html>

Compound Interest Calculation


Keywords (click to browse): javascript compound-interest compound-interest-calculator interest-calculator-in-javascript js web internet websites web-designing web-programming web-apps www