Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: Calculating Factorial in Python

Saturday, September 14, 2013

Calculating Factorial in Python

A simple program in Python to calculate the factorial of an natural number:

#File: fact.py
#A simple program
def main():
    print "Factorial Function"
    x=input("Enter a natural number:")
    l=1
    for i in range(x):
        l=l*x
        x=x-1
    print l
main()

Run the python interpreter from the folder where the file (fact.py) is residing and import it as shown below:


administrator@ubuntu:~/python$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fact
Factorial Function
Enter a natural number:350
123587405826548875014395199766546457224532073946919515879429330230093035357491314216934583295011178445941552109432761532449767761892237043444942213964090091669490545661255111334533069825455607852789836451585122902099649977304226794874840601811017764137584868137504975397325925882541777117706619490238363409254589994079334626893194608016888986949684994333459029365214555784862353939102567266745712846824819004146064184543888123533464975621179287075018586481357643313075153359002713294611632614208134036650116689052585573350955360246170451786972351365370405722036294385680478287278827977511411909071460914807681131728232182991517416470483157998067487290163200000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>>>


No comments:

Post a Comment