{"id":17215,"date":"2026-03-18T07:16:46","date_gmt":"2026-03-18T07:16:46","guid":{"rendered":"https:\/\/olive-ostrich-890936.hostingersite.com\/?p=17215"},"modified":"2026-04-23T18:11:51","modified_gmt":"2026-04-23T18:11:51","slug":"simple-recursive-factorial-using-a-native-type","status":"publish","type":"post","link":"https:\/\/gloritech.com.br\/?p=17215","title":{"rendered":"Simple Recursive Factorial Function Using a Native Type in C\/C++"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"17215\" class=\"elementor elementor-17215\" data-elementor-post-type=\"post\">\n\t\t\t\t<div class=\"elementor-element elementor-element-120629a4 e-con-full e-flex e-con e-parent\" data-id=\"120629a4\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-d19dba2 elementor-widget elementor-widget-html\" data-id=\"d19dba2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"html.default\">\n\t\t\t\t\t\n<div id=\"container-95d5aaf15d1dd1f718f28ca41fda8526\"><\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-71bf6359 elementor-widget elementor-widget-text-editor\" data-id=\"71bf6359\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>\u00a0#include&lt;iostream&gt;<\/p><p>\/\/ Simple recursive factorial using a native type (unsigned long long).<br \/>\/\/ Note: overflow for n &gt; 20 on 64-bit unsigned long long.<br \/>unsigned long long fact_recursive(unsigned n) {<br \/>return (n &lt;= 1) ? 1ULL : n * fact_recursive(n &#8211; 1);<br \/>}<\/p><p>int main() {<br \/>unsigned n;<br \/>std::cout &lt;&lt; &#8220;Enter a non-negative integer: &#8220;; if (!(std::cin &gt;&gt; n)) return 1;<\/p><p>if (n &gt; 20) {<br \/>std::cout &lt;&lt; &#8220;Warning: result will overflow unsigned long long for n &gt; 20.&#8221; &lt;&lt; std::endl;<br \/>}<\/p><p>unsigned long long result = fact_recursive(n);<br \/>std::cout &lt;&lt; n &lt;&lt; &#8220;! = &#8221; &lt;&lt; result &lt;&lt; std::endl;<br \/>return 0;<br \/>}<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3b24c62 elementor-widget elementor-widget-html\" data-id=\"3b24c62\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"html.default\">\n\t\t\t\t\t<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n  <meta charset=\"UTF-8\">\r\n  <title>Simple Recursive Factorial Using a Native Type<\/title>\r\n<\/head>\r\n<body>\r\n\r\n  <h1>Simple Recursive Factorial Using a Native Type<\/h1>\r\n\r\n  <h2>Introduction<\/h2>\r\n  <p>\r\n    The factorial function is a fundamental concept in mathematics and computer science.\r\n    It appears in combinatorics, probability, statistics, and algorithm design.\r\n    In simple terms, the factorial of a number <em>n<\/em> is the product of all positive integers less than or equal to <em>n<\/em>.\r\n    For example, 6! = 6 x 5 \u00d7 4 \u00d7 3 \u00d7 2 \u00d7 1 = 720.\r\n  <\/p>\r\n\r\n  <h2>Recursive Definition<\/h2>\r\n  <p>\r\n    Mathematically, factorial is defined as:\r\n  <\/p>\r\n  <p><strong>n! = n \u00d7 (n-1)! with 0! = 1<\/strong><\/p>\r\n  <p>\r\n    This definition is naturally recursive: to compute n!, you need to compute (n-1)!.\r\n  <\/p>\r\n\r\n  <h2>Recursive Implementation in C++<\/h2>\r\n  <pre><code>\r\n#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nlong long factorial_recursive(int n) {\r\n    if (n == 0) return 1;   \/\/ Base case\r\n    return n * factorial_recursive(n - 1);\r\n}\r\n\r\nint main() {\r\n    int num;\r\n    cout &lt;&lt; \"Enter a number: \";\r\n    cin &gt;&gt; num;\r\n\r\n    cout &lt;&lt; \"Recursive factorial of \" &lt;&lt; num &lt;&lt; \" = \"\r\n         &lt;&lt; factorial_recursive(num) &lt;&lt; endl;\r\n\r\n    return 0;\r\n}\r\n  <\/code><\/pre>\r\n  \r\n   <h2>Recursive Implementation in C<\/h2>\r\n  <pre><code>\r\n#include &lt;stdio.h&gt;\r\n\r\n\r\n    long long factorial_recursive(int n) {\r\n        if (n == 0) return 1;   \/\/ Base case\r\n        return n * factorial_recursive(n - 1);\r\n    }\r\n    \r\n    int main() {\r\n        int num;\r\n        cout &lt;&lt; \"Enter a number: \";\r\n        cin &gt;&gt; num;\r\n    \r\n        cout &lt;&lt; \"Recursive factorial of \" &lt;&lt; num &lt;&lt; \" = \"\r\n         &lt;&lt; factorial_recursive(num) &lt;&lt; endl;\r\n\r\n    return 0;\r\n}\r\n  <\/code><\/pre>\r\n\r\n  <h2>Iterative vs Recursive<\/h2>\r\n  <pre><code>\r\nlong long factorial_iterative(int n) {\r\n    long long result = 1;\r\n    for (int i = 1; i &lt;= n; i++) {\r\n        result *= i;\r\n    }\r\n    return result;\r\n}\r\n  <\/code><\/pre>\r\n  <ul>\r\n    <li><strong>Recursive:<\/strong> Elegant and close to the mathematical definition.<\/li>\r\n    <li><strong>Iterative:<\/strong> Often more efficient and avoids stack overflow issues.<\/li>\r\n  <\/ul>\r\n\r\n  <h2>Limitations<\/h2>\r\n  <p>\r\n    Factorials grow extremely fast. For example, 20! \u2248 2.43 \u00d7 10<sup>18<\/sup>.\r\n    This exceeds the range of a standard <code>int<\/code>.\r\n  <\/p>\r\n  <ul>\r\n    <li>Use <code>long long<\/code> for larger values.<\/li>\r\n    <li>Use <code>unsigned long long<\/code> for values up to about 20! safely.<\/li>\r\n    <li>For very large factorials, C\/C++ requires external libraries such as <strong>GMP<\/strong> or <strong>Boost.Multiprecision<\/strong>.<\/li>\r\n  <\/ul>\r\n  <\/ul>\r\n\r\n  <h2>Practical Applications<\/h2>\r\n  <ul>\r\n    <li>\ud83c\udfb2 Permutations: number of ways to arrange elements.<\/li>\r\n    <li>\ud83d\udcca Combinations: calculating probabilities.<\/li>\r\n    <li>\ud83d\udcbb Algorithms: complexity analysis and mathematical structures.<\/li>\r\n  <\/ul>\r\n\r\n  <h2>Conclusion<\/h2>\r\n  <p>\r\n    Recursion provides an elegant way to implement factorial, but it comes with limitations in performance and memory.\r\n    For real-world applications, iterative solutions or specialized libraries are often preferred.\r\n  <\/p>\r\n\r\n<\/body>\r\n<\/html>\r\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0a18713 elementor-widget elementor-widget-html\" data-id=\"0a18713\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"html.default\">\n\t\t\t\t\t\n<div id=\"container-95d5aaf15d1dd1f718f28ca41fda8526\"><\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>\u00a0#include&lt;iostream&gt; \/\/ Simple recursive factorial using a native type (unsigned long long).\/\/ Note: overflow for n &gt; 20 on 64-bit unsigned long long.unsigned long long fact_recursive(unsigned n) {return (n &lt;= 1) ? 1ULL : n * fact_recursive(n &#8211; 1);} int main() {unsigned n;std::cout &lt;&lt; &#8220;Enter a non-negative integer: &#8220;; if (!(std::cin &gt;&gt; n)) return 1; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17215","post","type-post","status-publish","format-standard","hentry","category-info-e-tech"],"_links":{"self":[{"href":"https:\/\/gloritech.com.br\/index.php?rest_route=\/wp\/v2\/posts\/17215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gloritech.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gloritech.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gloritech.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gloritech.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17215"}],"version-history":[{"count":29,"href":"https:\/\/gloritech.com.br\/index.php?rest_route=\/wp\/v2\/posts\/17215\/revisions"}],"predecessor-version":[{"id":18596,"href":"https:\/\/gloritech.com.br\/index.php?rest_route=\/wp\/v2\/posts\/17215\/revisions\/18596"}],"wp:attachment":[{"href":"https:\/\/gloritech.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gloritech.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gloritech.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}