Calculate a Number Factorial in Python — pythonpip.com

Using for Loop

n = input("Enter a number: ") factorial = 1 if int(n) >= 1: for i in range (1,int(n)+1): factorial = factorial * i print("Factorail of ",n , " is : ",factorial)
Enter a number: 4 Factorial of 4 is : 24

Calculate Factorial Using Recurssion

num = input("Enter a number: ") def fact_recursive(n): if n == 1: return n elif n < 1: return ("NA") else: return n*fact_recursive(n-1) print("The factorial of ", num, " is : ") print (fact_recursive(int(num)))
Enter a number: 5 The factorial of 5 is : 120

Calculate Factorial Using math.factorial()

import math num = input("Enter a number: ") print("The factorial of ", num, " is : ") print(math.factorial(int(num)))
Enter a number: 5 The factorial of 5 is : 120

Factorial of an integer with NumPy in Python

import numpy as np num = input("Enter a number: ") factorial = np.math.factorial(int(num)) print("Factorail of ",num , " is : ",factorial)
Enter a number: 6 Factorail of 6 is : 720

--

--

Hey, I am Parvez Alam. A software developer since 2009. I am a programming addict. I love learning and sharing knowledge. https://www.phpflow.com/

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store