{"id":184813,"date":"2025-01-21T16:36:49","date_gmt":"2025-01-21T16:36:49","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=184813"},"modified":"2025-01-21T16:36:51","modified_gmt":"2025-01-21T16:36:51","slug":"for-c-language-1-write-a-c-program-to-calculate-the-factorial-of-a-given-integer-using-recursion","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/01\/21\/for-c-language-1-write-a-c-program-to-calculate-the-factorial-of-a-given-integer-using-recursion\/","title":{"rendered":"For C Language 1) Write a C program to calculate the factorial of a given integer using recursion"},"content":{"rendered":"\n<p>For C Language 1) Write a C program to calculate the factorial of a given integer using recursion ? 2)Implement a function in C to check if a number is prime or not ? For C++ Language 1)Write a C++ class for a basic rectangle shape with functions to calculate area and perimeter ? 2)Implement a C++ program to find the factorial of a given number using a recursive function<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<p>Below are the programs and explanations for each task.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>C Language Programs<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Factorial of a number using recursion<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint factorial(int n) {\n    if (n == 0 || n == 1)\n        return 1; \/\/ Base case\n    return n * factorial(n - 1); \/\/ Recursive case\n}\n\nint main() {\n    int num;\n    printf(\"Enter a number to find its factorial: \");\n    scanf(\"%d\", &amp;num);\n\n    if (num &lt; 0)\n        printf(\"Factorial is not defined for negative numbers.\\n\");\n    else\n        printf(\"Factorial of %d is %d\\n\", num, factorial(num));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Function to check if a number is prime<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint isPrime(int n, int i) {\n    if (n &lt;= 2)\n        return n == 2; \/\/ Base case\n    if (n % i == 0)\n        return 0; \/\/ Not prime\n    if (i * i &gt; n)\n        return 1; \/\/ Prime\n    return isPrime(n, i + 1); \/\/ Recursive check\n}\n\nint main() {\n    int num;\n    printf(\"Enter a number to check if it is prime: \");\n    scanf(\"%d\", &amp;num);\n\n    if (num &lt;= 1)\n        printf(\"%d is not a prime number.\\n\", num);\n    else if (isPrime(num, 2))\n        printf(\"%d is a prime number.\\n\", num);\n    else\n        printf(\"%d is not a prime number.\\n\", num);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>C++ Language Programs<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Rectangle class with area and perimeter functions<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Rectangle {\nprivate:\n    double length;\n    double width;\n\npublic:\n    Rectangle(double l, double w) : length(l), width(w) {}\n\n    double area() {\n        return length * width;\n    }\n\n    double perimeter() {\n        return 2 * (length + width);\n    }\n};\n\nint main() {\n    double length, width;\n    cout &lt;&lt; \"Enter length and width of the rectangle: \";\n    cin &gt;&gt; length &gt;&gt; width;\n\n    Rectangle rect(length, width);\n    cout &lt;&lt; \"Area: \" &lt;&lt; rect.area() &lt;&lt; endl;\n    cout &lt;&lt; \"Perimeter: \" &lt;&lt; rect.perimeter() &lt;&lt; endl;\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Factorial using recursion in C++<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint factorial(int n) {\n    if (n == 0 || n == 1)\n        return 1; \/\/ Base case\n    return n * factorial(n - 1); \/\/ Recursive case\n}\n\nint main() {\n    int num;\n    cout &lt;&lt; \"Enter a number to find its factorial: \";\n    cin &gt;&gt; num;\n\n    if (num &lt; 0)\n        cout &lt;&lt; \"Factorial is not defined for negative numbers.\" &lt;&lt; endl;\n    else\n        cout &lt;&lt; \"Factorial of \" &lt;&lt; num &lt;&lt; \" is \" &lt;&lt; factorial(num) &lt;&lt; endl;\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation (300 Words)<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Factorial Programs<\/strong><\/h4>\n\n\n\n<p>Both C and C++ programs use recursion to compute the factorial. The base case stops the recursion when the input is <code>0<\/code> or <code>1<\/code>, as their factorial is <code>1<\/code>. The recursive case reduces the problem by multiplying the current number with the factorial of <code>(n-1)<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Prime Number Check<\/strong><\/h4>\n\n\n\n<p>The prime-checking function uses recursion in C. It starts dividing the number from <code>2<\/code> up to the square root of the number (<code>i * i &gt; n<\/code>). If the number is divisible by any number in this range, it is not prime.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Rectangle Class<\/strong><\/h4>\n\n\n\n<p>In the C++ class, encapsulation is used to manage <code>length<\/code> and <code>width<\/code>. Methods <code>area<\/code> and <code>perimeter<\/code> provide functionality to calculate the respective values.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Comparison<\/strong><\/h4>\n\n\n\n<p>Both C and C++ programs demonstrate recursion but with differences in syntax and paradigms. The class-based approach in C++ allows modeling real-world entities (e.g., Rectangle), while C uses a more procedural style. These programs highlight recursion, object-oriented programming (OOP), and functional decomposition.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For C Language 1) Write a C program to calculate the factorial of a given integer using recursion ? 2)Implement a function in C to check if a number is prime or not ? For C++ Language 1)Write a C++ class for a basic rectangle shape with functions to calculate area and perimeter ? 2)Implement [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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":"default","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":[25],"tags":[],"class_list":["post-184813","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/184813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=184813"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/184813\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=184813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=184813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=184813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}