{"id":25922,"date":"2017-10-25T21:23:56","date_gmt":"2017-10-25T15:53:56","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25922"},"modified":"2017-10-25T21:23:56","modified_gmt":"2017-10-25T15:53:56","slug":"c-programming-efficient-program-calculate-ex","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-efficient-program-calculate-ex\/","title":{"rendered":"C Programming-Efficient program to calculate e^x"},"content":{"rendered":"<p>The value of Exponential Function e^x can be expressed using following Taylor Series.<\/p>\n<p>e^x = 1 + x\/1! + x^2\/2! + x^3\/3! + &#8230;&#8230;<\/p>\n<p>How to efficiently calculate the sum of above series?<br \/>\nThe series can be re-written as<\/p>\n<p>e^x = 1 + (x\/1) (1 + (x\/2) (1 + (x\/3) (&#8230;&#8230;..) ) )<br \/>\nLet the sum needs to be calculated for n terms, we can calculate sum using following loop.<\/p>\n<p>for (i = n &#8211; 1, sum = 1; i &gt; 0; &#8211;i )<br \/>\nsum = 1 + x * sum \/ i;<br \/>\nFollowing is implementation of the above idea.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Efficient program to calculate e raise to the power x<br\/>#include &lt;stdio.h&gt;<br\/> <br\/>\/\/Returns approximate value of e^x using sum of first n terms of Taylor Series<br\/>float exponential(int n, float x)<br\/>{<br\/>    float sum = 1.0f; \/\/ initialize sum of series<br\/> <br\/>    for (int i = n - 1; i &gt; 0; --i )<br\/>        sum = 1 + x * sum \/ i;<br\/> <br\/>    return sum;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    int n = 10;<br\/>    float x = 1.0f;<br\/>    printf(&quot;e^x = %f&quot;, exponential(n, x));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>e^x = 2.718282<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Programming Efficient program to calculate e^x &#8211; Mathematical Algorithms &#8211; The value of Exponential Function e^x can be expressed using Taylor Series. <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866,1,74058],"tags":[76145,76166,76143,76157,73060,75065,76137,76131,76163,76160,76161,76164,74982,76139,74184,70849,76136,76151,76146,76150,76158,76154,76148,76156,76144,76142,76152,76159,76135,76138,76155,76141,76129],"class_list":["post-25922","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","category-mathematical-algorithms","tag-how-to-program-in-python","tag-how-to-program-python","tag-how-to-write-a-program-in-python","tag-how-to-write-program-in-python","tag-list-count-python","tag-loop-programming","tag-math-for-programmers","tag-math-programs","tag-programming-math","tag-python-for-loop","tag-python-for-loop-list","tag-python-how-to-program","tag-python-loop-example","tag-python-programming-exercises","tag-python-repeat","tag-simple-c-programs-for-beginners","tag-software-design-process","tag-vb-net-notes","tag-vb-programs-with-coding-and-output","tag-visual-basic-6-0-notes-pdf","tag-visual-basic-controls","tag-visual-basic-form","tag-visual-basic-notes","tag-visual-basic-notes-pdf","tag-visual-basic-programming","tag-visual-basic-programs-with-codes-and-output","tag-visual-basic-theory-notes","tag-visual-basic-theory-notes-pdf","tag-visual-programming-notes","tag-what-is-python-used-for-in-programming","tag-what-is-visual-basic-used-for","tag-while-loop-in-python","tag-who-invented-c-language"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25922"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25922\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}