{"id":2666,"date":"2017-09-02T00:01:43","date_gmt":"2017-09-02T07:01:43","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2666"},"modified":"2017-09-09T08:10:07","modified_gmt":"2017-09-09T15:10:07","slug":"compound-interest-calculations","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2666","title":{"rendered":"Compound Interest Calculations"},"content":{"rendered":"<p>You would think that simple interest (from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2658\">last week&#8217;s Lesson<\/a>) would be enough. After all, if you&#8217;re investing, you make money; if you&#8217;re paying back a loan, you can figure the math. That aspect of interest wasn&#8217;t enough for mathematical and banking geniuses, who devised a second type of interest calculation, which is &#8220;interest on the interest.&#8221;<br \/>\n<!--more--><br \/>\nYes, compound interest sounds evil. Compounds aren&#8217;t readily associated with nice things, but the word &#8220;compound&#8221; is also a verb, <em>to compound<\/em>.<\/p>\n<p>The notion is this: If you get paid 3 percent interest on your investment, shouldn&#8217;t that interest also earn interest? During the year, as the interest accumulates, it&#8217;s added to the base or principal amount. That sounds like fun!<\/p>\n<p>Calculating compound interest has been a topic in mathematics for centuries. Rather than bother with the detailed specifics or boring history, Figure 1 details the formula for compound interest, along with the common variable names used in math, spreadsheets, and other calculations.<\/p>\n<div id=\"attachment_2669\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2669\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/09\/0902-figure1_compound-interest.png\" alt=\"\" width=\"550\" height=\"334\" class=\"size-full wp-image-2669\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/09\/0902-figure1_compound-interest.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/09\/0902-figure1_compound-interest-300x182.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/09\/0902-figure1_compound-interest-494x300.png 494w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-2669\" class=\"wp-caption-text\">Figure 1. Compound Interest formula.<\/p><\/div>\n<p>If you&#8217;re a math nerd, you might find it interesting to see how the formula was concocted. Otherwise, long-story-short: Because the interest is applied to itself, you need an exponential operator. That&#8217;s how the formula works.<\/p>\n<p>You duty as a programmer is to convert the compound interest formula into the C language, which I&#8217;ve done in the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    int i,n,t;\r\n    float p,fv,r;\r\n\r\n    puts(\"Compound Interest Calculator\");\r\n\r\n    <span class=\"comments\">\/* gather data *\/<\/span>\r\n    printf(\"Initial balance: $\");\r\n    scanf(\"%f\",&p);\r\n    printf(\"Interest rate percentage: \");\r\n    scanf(\"%d\",&i);\r\n    r = (float)i\/100;\r\n    printf(\"Compunding times per year: \");\r\n    scanf(\"%d\",&n);\r\n    printf(\"Term in years: \");\r\n    scanf(\"%d\",&t);\r\n\r\n    <span class=\"comments\">\/* calculate *\/<\/span>\r\n    fv = p * pow( ( 1 + (r\/n) ), t*n );\r\n    printf(\"Future value is %.2f\\n\",fv);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code gathers input from the user similar to the simple interest calculator introduced for last week&#8217;s Lesson. Here I&#8217;m using shorter variable names to match the formula presented in Figure 1.<\/p>\n<p>The calculation is made at Line 23. The <em>pow()<\/em> function requires the <code>math.h<\/code> header (Line 2). It handles the parentheses part of the formula, which packs into the function&#8217;s first argument. The <code>t*n<\/code> exponent is the second argument.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>Compound Interest Calculator\r\nInitial balance: $1000\r\nInterest rate percentage: 5\r\nCompunding times per year: 1\r\nTerm in years: 10\r\nFuture value is 1628.89<\/code><\/pre>\n<p>If you increase the number of compounding periods to monthly, you can eek out a bit more income:<\/p>\n<pre><code>Compound Interest Calculator\r\nInitial balance: $1000\r\nInterest rate percentage: 5\r\nCompunding times per year: 12\r\nTerm in years: 10\r\nFuture value is 1647.02<\/code><\/pre>\n<p>Eventually, as the number of compounding periods tends to infinity, the value returned flattens out. This effect is related to a mathematical constant, Euler&#8217;s number <em>e<\/em>. I&#8217;ll explore that calculation in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2680\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wait! It&#8217;s not as boring as it sounds. In fact, if you want to make money in your life, it&#8217;s a useful thing to know. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2666\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2666","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2666"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2666\/revisions"}],"predecessor-version":[{"id":2705,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2666\/revisions\/2705"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}