{"id":2658,"date":"2017-08-26T00:01:23","date_gmt":"2017-08-26T07:01:23","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2658"},"modified":"2017-09-02T07:34:21","modified_gmt":"2017-09-02T14:34:21","slug":"of-great-interest","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2658","title":{"rendered":"Of Great Interest"},"content":{"rendered":"<p>I&#8217;m frustrated, as are many investors, at the lack of interest generated in &#8220;savings&#8221; accounts these days. It&#8217;s pitiful. The interest formula is based on solid mathematics, which is easy to code in C. Doing so may help you understand the concept of interest and how it works.<br \/>\n<!--more--><br \/>\nFirst, some definitions.<\/p>\n<p>Interest is money paid on a debt or money received from an investment. To be upbeat, I&#8217;m going to frame this discussion on money received from an investment: You put $100 in the bank and every month your statement shows a little bit extra added. That&#8217;s interest.<\/p>\n<p>Interest has two types: simple and compound. These two terms fold into the second topic, which is how the interest is calculated. Simple interest is the easy one to figure:<\/p>\n<p><code>( interest_rate * initial_balance * time_periods ) \/ payment_frequency<\/code><\/p>\n<p>Say the interest rate is 3 percent, which would be generous right now. The initial balance is $100, held for 12 months. The payment frequency is annually, or once every 12 months:<\/p>\n<p><code>( 0.03 * 100 * 12 ) \/ 12 = 3<\/code><\/p>\n<blockquote><p>It&#8217;s important to remember that &#8220;3 percent&#8221; is 0.03 and not 3.0<\/p><\/blockquote>\n<p>An investment of $100, held for a year at a simple interest rate of 3 percent paid annually, yields $3 at the end of the term; your initial $100 investment would be worth $103.<\/p>\n<p>When the interest payment is made monthly,the <code>payment_frequency<\/code> variable changes to 1:<\/p>\n<p><code>( 0.03 * 100 * 12 ) \/ 1 = 36<\/code><\/p>\n<p>The <code>time_periods<\/code> variable, or term, is still 12 months, one year. With this change, after a year at 3 percent interest paid monthly, the yield is $36; you end up with $136. Yes, that&#8217;s a tremendous investment, and it&#8217;s why most interest is calculated annually and paid monthly.<\/p>\n<p>The following code is a simple interest calculator. It fetches the desired values from standard input and generates a result.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    float initial_balance,interest_rate,interest_earned;\r\n    int interest_input,payment_frequency,time_periods;\r\n\r\n    puts(\"Simple Interest Calculator\");\r\n\r\n    <span class=\"comments\">\/* gather data *\/<\/span>\r\n    printf(\"Enter the initial balance: $\");\r\n    scanf(\"%f\",&initial_balance);\r\n    printf(\"Enter the interest rate percentage: \");\r\n    scanf(\"%d\",&interest_input);\r\n    interest_rate = (float)interest_input\/100;\r\n    printf(\"Enter the payment frequency in months: \");\r\n    scanf(\"%d\",&payment_frequency);\r\n    printf(\"Enter the term of the loan in months: \");\r\n    scanf(\"%d\",&time_periods);\r\n\r\n    <span class=\"comments\">\/* calculate *\/<\/span>\r\n    interest_earned =\r\n        (interest_rate * initial_balance * time_periods)\r\n        \/payment_frequency;\r\n\r\n    <span class=\"comments\">\/* display results *\/<\/span>\r\n    printf(\"The ending balance will be $%.2f\\n\",\r\n            initial_balance+interest_earned);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Data is gathered in Lines 10 through 19. At Line 13, the interest rate is prompted as a percent and an <em>int<\/em> value fetched. Getting input here is an issue in many interest-calculating programs, because you don&#8217;t know whether the user will type <strong>3<\/strong> or <strong>0.03<\/strong> for &#8220;3 percent.&#8221; The code assumes integer input, and Line 15 makes the conversion to a <em>float<\/em> value.<\/p>\n<p>The simple interest is calculated in a single statement that spans Lines 22 through 24. The long variable names make the code readable, but the statement long. The split format (made possible because C ignores the white space) looks more like the traditional simple interest formula.<\/p>\n<p>The result is displayed at Line 27.<\/p>\n<p>Here is a sample run:<\/p>\n<pre><code>Simple Interest Calculator\r\nEnter the initial balance: $100\r\nEnter the interest rate percentage: 3\r\nEnter the payment frequency in months: 12\r\nEnter the term of the loan in months: 12\r\nThe ending balance will be $103.00<\/code><\/pre>\n<p>And:<\/p>\n<pre><code>Simple Interest Calculator\r\nEnter the initial balance: $100\r\nEnter the interest rate percentage: 3\r\nEnter the payment frequency in months: 1\r\nEnter the term of the loan in months: 12\r\nThe ending balance will be $136.00<\/code><\/pre>\n<p>Remember, these are simple interest calculations. The second type of interest is compound interest, which is a bit more involved and confusing to calculate. I&#8217;ll put off that topic to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2666\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s perhaps the most common and frustrating calculation that most people pay attention to &mdash; or not. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2658\">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-2658","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\/2658","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=2658"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2658\/revisions"}],"predecessor-version":[{"id":2692,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2658\/revisions\/2692"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}