Python Factorial Example

Python factorial example using recursion method as well as python inbuilt recursion function.


#Python factorial

#Create python factorial function using recursion where a function calls itself
def factorial_function(number):
    if number == 0:
        return 1
    else:
        return number * factorial_function(number - 1)


print("{} factorial is {}".format(4, factorial_function(4)))
print("{} factorial is {}".format(0, factorial_function(0)))

---------------------------------------------------------------------
#Output
4 factorial is 24
0 factorial is 1

#Below uses the python inbuilt factorial function
import math
print(math.factorial(4))
print(math.factorial(0))


----------------------------------------------------------------------------------------------
#Output
24
1

Comments

Popular posts from this blog

How to write to a file in Kotlin

Python Tkinter example

Kotlin - Display Date picker dialog using EditText