Tuesday, January 4, 2022

Python for Beginners: Find Factorial Of A Number In Python

In mathematics, we can perform different operations on any given number. One such operation is finding the factorial of a number. In this article, we will discuss what a factorial is and how we can find the factorial of a number in python.

What Is Factorial Of A Number?

The factorial of a number N is defined as the product of all the numbers from 1 to N. In other words, to find the factorial of a given number N, we just have to multiply all the numbers from 1 to N.

For instance, the factorial of 5 will be calculated as 1x2x3x4x5 i.e 120. Similarly, the factorial of 6 will be 1x2x3x4x5x6 i.e. 720.

The factorial of 0 has been defined to be 1. On the other hand, factorial is not defined for negative integers. 

Having known this much, let us devise an algorithm to find the factorial of a given number.

Find Factorial Of A Number In Python

To find the factorial of any given number N, we just have to multiply all the numbers from 1 to N. Before doing this, we will first check if the given number is a negative number. If yes, we will inform the user that factorial is not defined for the given number. If the input number is 0, we will say that factorial is 1.

In the case of positive integers, we can define a variable “myFact” and find the product of all the numbers from 1 to N using the for loop in python as follows.

def factorial(N):
    if N < 0:
        return -1
    if N == 0:
        return 1
    myFact = 1
    for i in range(1, N + 1):
        myFact = myFact * i
    return myFact


input_number = 0
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))
input_number = 6
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))
input_number = -10
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))

Output:

Factorial of 0 is 1.
Factorial of 6 is 720.
Factorial not defined for negative number -10.

Alternatively, we can use a while loop to find the factorial. Here, we will use a count variable that starts from 1, goes till N, and is incremented by 1 during each iteration of the while loop. In each iteration, we will multiply the count to the product of previous numbers. Thus, we can calculate the factorial of a number using a while loop in python as follows.

def factorial(N):
    if N < 0:
        return -1
    if N == 0:
        return 1
    myFact = 1
    count = 1
    while count <= N:
        myFact = myFact * count
        count = count + 1
    return myFact


input_number = 0
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))
input_number = 6
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))
input_number = -10
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))

Output:

Factorial of 0 is 1.
Factorial of 6 is 720.
Factorial not defined for negative number -10.

Conclusion

In this article, we have discussed the factorial of a number. We have also implemented programs to find the factorial of a number using the for loop and the while loop in python.  To learn more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.

The post Find Factorial Of A Number In Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...