How to Calculate Factorials in Python


Here this example tells how to calculate factorials in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

# define the function of calculating factorial
def factorial(a):
    if a>=1:
        return factorial(a-1)*a
    else:
        return 1

# sum up factorials
sum = 0
a = int(input("please input your num: "))
for i in range(1,a):
    sum += factorial(i)

print(sum)

Output:

please input your num:  9
46233
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments