{"id":6694,"date":"2021-05-22T10:14:13","date_gmt":"2021-05-22T04:44:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=6694"},"modified":"2021-11-22T18:38:44","modified_gmt":"2021-11-22T13:08:44","slug":"python-program-to-find-the-factorial-of-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-factorial-of-a-number\/","title":{"rendered":"Python Program to Find the Factorial of a Number"},"content":{"rendered":"
The product of all positive integers less than or equal to n is the factorial of a non-negative integer n, denoted by n! in mathematics:<\/p>\n
n! = n * (n – 1) *(n – 2) * . . . . . . . . . . 3 * 2 * 1.<\/strong><\/p>\n 4 != 4 * 3 * 2 * 1<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Given a number , the task is to find the factorial of the given number<\/p>\n There are several ways to find factorial of a number in python some of them are:<\/p>\n Explore more instances related to python concepts from Python Programming Examples<\/a> Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n To find the factorial of a given number, we will use recursion. We defined the factorial(num) function, which returns 1\u00a0if the entered value is 1\u00a0and 0\u00a0otherwise, until we get the factorial of a given number.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Python has a built-in function<\/p>\n which returns the factorial of given number.<\/p>\n Note:<\/strong> factorial function is available in math module<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Related Programs<\/strong>:<\/p>\n Factorial of a number: The product of all positive integers less than or equal to n is the factorial of a non-negative integer n, denoted by n! in mathematics: n! = n * (n – 1) *(n – 2) * . . . . . . . . . . 3 * 2 * 1. …<\/p>\nnumber = 7<\/pre>\n
Factorial of 7 = 5040<\/pre>\n
Finding Factorial of a Number<\/h2>\n
\n
Method #1:Using for loop(Without Recursion)<\/h3>\n
\n
# given number\r\nnum = 7\r\n# initializing a variable res with 1\r\nres = 1\r\n# Traverse from 1 to n\r\nfor i in range(1, num+1):\r\n res = res*i\r\n# print the factorial\r\nprint(\"Factorial of\", num, \"=\", res)\r\n<\/pre>\n
Factorial of 7 = 5040<\/pre>\n
Method #2: Using recursion<\/h3>\n
# function which returns the factorial of given number\r\ndef facto(num):\r\n if(num == 1 or num == 0):\r\n return 1\r\n else:\r\n return num*facto(num-1)\r\n\r\n\r\n# given number\r\nnum = 7\r\n# passing the given num to facto function which returns the factorial of the number\r\nres = facto(num)\r\n# print the factorial\r\nprint(\"Factorial of\", num, \"=\", res)\r\n<\/pre>\n
Factorial of 7 = 5040<\/pre>\n
Method #3 : Using Built in Python functions<\/h3>\n
factorial (number)<\/code><\/p>\n
# import math module\r\nimport math\r\n# given number\r\nnum = 7\r\n# finding factorial using Built in python function\r\nres = math.factorial(num)\r\n# print the factorial\r\nprint(\"Factorial of\", num, \"=\", res)\r\n<\/pre>\n
Factorial of 7 = 5040<\/pre>\n
\n
\n