×

Python program to find factorial of a given number

Python program to find factorial of a given number

Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to calculate the factorial. It is represented by (!).

Example: Factorial of 5 is 5*4*3*2*1 = 120

Source Code

The following is a source code to get the factorial of a given number.

#Program to find factorial of the given number                                                                          
#Take input from user   
number = int(input("Enter the number to find factorial:"))                                    
 count = 1   
 #Check the number is positive, negative, or zero 
if number < 0:   
print("Factorial for negative number doesn't exist.") 
 elif number==0:
print("The factorial will be 1") 
else:  
while(number>1): 
count *=number 
number -= 1 
print("The factorial of an entered number is", count) 

Output

CASE 1:

Python program to find factorial of a given number

CASE 2:

Python program to find factorial of a given number

Explanation:

In this program, a user asks to enter a number whose factorial will be calculated. This number will store in a variable named number. Next, we declare the variable count and initialized its value 1. Now, we will check the number is positive, negative, or zero using if....elif...else statements. If the number is greater than 1, the count's value is increased by multiplying the numbers with the value stored in the count. After each iteration, the value of the number will decrease by 1. When the iteration stopped, the count variable's updated value will be printed, which is the factorial of the number that we have entered to calculate.

Using Recursion

The following is a source code to get factorial number using recursion:

 #Program to calculate factorial of a number using recursion  
 def factorial(n): 
if n == 1: 
        return n 
else:   
        return n*factorial(n-1) 
 number = int(input("Enter the number: "))
 # check if the number is negative     
 if number < 0: 
    print("Factorial for negative number doesn't exist")   
 elif number == 0: 
    print("The factorial of 0 is 1") 
else:    
    print("The factorial of", number, "is", factorial(number))  

Output

CASE 1:

Python program to find factorial of a given number

CASE 2:

Python program to find factorial of a given number

Explanation:

In this program, the number variable is declared to store the numeric value. This number is then passed to the recursive function factorial() to calculate the factorial. The recursive function calls itself and multiplies the number with the factorial of the number below it until it becomes equal to one. If the number is equal to 1, it will display the value stored in a number variable on the screen.


Related Topics

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

Python Euclidean Distance

Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for...

1 minute read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

Loan calculator using Tkinter in Python

Tkinter: Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is...

4 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Python Dictionary setdefault() method

Python Dictionary setdefault() method The dictionary.setdefault () method in Python returns the value of the item with the specified key. Syntax dictionary.setdefault(keyname, value) Parameter keyname- This parameter represents the keyname of the item you want...

1 minute read.

Python Decorator

Python Decorator: A Decorator is an interesting feature of Python that helps the user design patterns and insert a new functionality to an existing object without making any modifications in...

6 minutes read.

Python Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you...

4 minutes read.

Python Strong Number

Strong Number- Logic Divide each of the number's digits into separate units to determine if it is a strong number or not.The factorial of each digit must then be determined. The...

5 minutes read.

Iterators in Python

Introduction In Python, an iterator is defined as an object that enables traversing through all the values of a collection. It contains the countable number of values. The iterator is utilized to...

4 minutes read.

Python variance() function

Variance The variance is the average of the square deviations from the mean. The variance will measure the spread of the dataset from its mean or median value. The greater the...

4 minutes read.

Python len() function

Python len() function The len() function in Python  returns the number of items in an object. Syntax len(s) Parameter s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or...

1 minute read.

Python math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

Python Tuple count() Method

Python Tuple count() Method The tuple.count() method in Python returns the number of times a specified value appears in the tuple. Syntax tuple.count(value) Parameter value- This parameter represents the element to search in the tuple Return This...

1 minute read.

Python logging Module

Python logging Module Introduction By logging word we understand the tracking of the events which happens when we run some software. Logging process is very important part for developing software, debugging...

12 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

Python memoryview() Function

Python memoryview() Function The memoryview() function in Python returns a memory view object from a specified object. Syntax memoryview(obj) Parameter obj: This parameter represents a Bytes object or a bytearray object. Return This function returns a “memory view” object...

1 minute read.