{"id":1796,"date":"2016-03-08T00:01:15","date_gmt":"2016-03-08T08:01:15","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1796"},"modified":"2016-03-05T08:32:55","modified_gmt":"2016-03-05T16:32:55","slug":"appreciation-for-depreciation-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1796","title":{"rendered":"Appreciation for Depreciation &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1784\">Exercise<\/a> is one of those old, programming warhorses: Calculate a depreciation schedule. At the simplest level, the solution is a mathematical loop and keeps subtracting a given percentage from a value.<br \/>\n<!--more--><br \/>\nMy no-brainer solution is shown at the bottom of this post. Most of the code collects the input and generates the text output. The core of the depreciation process is a <em>for<\/em> loop:<\/p>\n<pre class=\"screen\">\r\nfor(y=0;y&lt;=years;y++)\r\n{\r\n    printf(\"Year %2d: $%.2f\\n\",y,cost);\r\n    cost = cost - (cost * depreciation);\r\n}<\/pre>\n<p><code>years<\/code> is an <em>int<\/em> variable representing the number of years to depreciate; <code>cost<\/code> and <code>depreciation<\/code> are <em>float<\/em> variables representing the initial cost and the percentage to depreciate, respectively.<\/p>\n<p>The <em>for<\/em> loop starts with the current year, &#8220;year zero,&#8221; which is why the loop&#8217;s termination condition is <code>&lt;=<\/code> and not just <code>&lt;<\/code>. For each year, the cost is reduced by the current value minus the percentage of depreciation.<\/p>\n<p>Here&#8217;s the full code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    float cost,depreciation;\r\n    int years,y;\r\n\r\n    printf(\"Enter initial cost: $\");\r\n    scanf(\"%f\",&amp;cost);\r\n    printf(\"Enter annual depreciation as a decimal: \");\r\n    scanf(\"%f\",&amp;depreciation);\r\n    printf(\"Enter years to depreciate: \");\r\n    scanf(\"%d\",&amp;years);\r\n\r\n    puts(\"\\nDepreciation Schedule\");\r\n    puts(\"=====================\");\r\n    for(y=0;y&lt;=years;y++)\r\n    {\r\n        printf(\"Year %2d: $%.2f\\n\",y,cost);\r\n        cost = cost - (cost * depreciation);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>As always, other solutions are possible. The big difference between this code and others is most likely with the output, which could get really fancy. Another option would be to hone the input statements, for example, to allow dollar amounts to be typed with <strong>$ ,<\/strong> and <strong>.<\/strong> characters or to allow the percentage to be input as a string such as <strong>10%<\/strong>. These modifications would require specific input functions, which I&#8217;ve demonstrated in other Lessons and Exercises.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise is one of those old, programming warhorses: Calculate a depreciation schedule. At the simplest level, the solution is a mathematical loop and keeps subtracting a given percentage from a value.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1796","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1796","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=1796"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1796\/revisions"}],"predecessor-version":[{"id":1812,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1796\/revisions\/1812"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}