{"id":24277,"date":"2023-01-22T14:56:37","date_gmt":"2023-01-22T09:26:37","guid":{"rendered":"https:\/\/copyassignment.com\/?p=24277"},"modified":"2023-01-22T14:56:39","modified_gmt":"2023-01-22T09:26:39","slug":"python-factorial-of-a-number-using-recursion","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/python-factorial-of-a-number-using-recursion\/","title":{"rendered":"Python | Factorial of a number using Recursion"},"content":{"rendered":"\n<p>Using the concept of recursion, we have two methods to solve this problem. Let us take a look at that method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 1: Using factorial<\/h2>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"false\" data-copy=\"true\">def get_factorial(n):  \n   if n == 1:  \n       return n  \n   else:  \n       return n*get_factorial(n-1)  \n\nnum = int(input(\"Enter a number whose factorial you want: \"))  \n\nif num &lt; 0:  \n   print(\"Opps!, The number you entered is negative. Factorial does not exist for negative numbers\")  \nelif num == 0:  \n   print(\"The factorial of 0 is 1\")  \nelse:  \n   print(\"The factorial of\",num,\"is\",get_factorial(num))<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter a number whose factorial you want: 5\nThe factorial of 5 is 120\n\nEnter a number whose factorial you want: -1\nOpps!, The number you entered is negative. Factorial does not exist for negative numbers<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>The concept of finding factorial is quite similar to that of finding Fibonacci. Here we simply took the input from the user and passed on that to the function. The main idea of the factorial of a particular number is to reduce the number by 1 and multiply all of them until 1.  So using for loop we reduced n each time in the loop until the value of n comes down to 1. At the same time, the value computed is stored and multiplied by the value computed by the next for loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 2: Using a one-liner if statement<\/h2>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"false\" data-copy=\"true\">def get_factorial(n):\n    return 1 if (n==1 or n==0) else n * get_factorial(n - 1)\n\nnum = int(input(\"Enter a number whose factorial you want: \"))  \n\nif num &lt; 0:  \n   print(\"Opps!, The number you entered is negative. Factorial does not exist for negative numbers\")  \nelif num == 0:  \n   print(\"The factorial of 0 is 1\")  \nelse:  \n   print(\"The factorial of\",num,\"is\",get_factorial(num))  <\/pre><\/div>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>The approach is the same as the above method but the only difference is that here we used a one-liner if statement and nothing else.<\/p>\n\n\n\n<p>We hope this article was helpful in some ways.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using the concept of recursion, we have two methods to solve this problem. Let us take a look at that method. Method 1: Using factorial&#8230;<\/p>\n","protected":false},"author":62,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1930],"tags":[],"class_list":["post-24277","post","type-post","status-publish","format-standard","hentry","category-allcategorites","category-programs-in-python","wpcat-22-id","wpcat-1930-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/24277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=24277"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/24277\/revisions"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=24277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=24277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=24277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}