×

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

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Python coding platform

Python is a popular general-purpose programming language with many applications. High-level data structures, datatypes, dynamic binding, and many other features make it useful for both designing complex applications and "glue...

6 minutes read.

Python Set difference() Method

Python Set difference() Method The set.difference() method in Python returns the set difference of two sets(A-B). Syntax set.difference(set1) Parameter set- This argument represents a set (minuend) set1- This arguments represents a set(subtrahend) Return This method returns the difference of the two specified...

2 minutes read.

Python email utils

Python is a popular programming language in this growing world. There are many resources to learn python online without spending a single penny. In this article, we will talk about a...

4 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.

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 minutes read.

Ternary operators in python

Starting from Python version 2.5, ternary operators are also known as Conditional operators. Using these operators, we can evaluate any problem based on a condition. It is an alternative and...

4 minutes read.

Sklearn in Python

Scikit-learn or sklearn is a machine learning library used in Python that provides many unsupervised and supervised learning tools and algorithms. David Cournapeau first created it as a 2007 Google...

7 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Python slice()

Python slice() class The slice() class return a slice object representing the set of indices specified by range(start, stop, step).  Syntax class slice(stop)          or class slice(start, stop[, step]) Parameter Start: This parameter represents an integer number specifying at...

1 minute read.

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

Class and Static Methods in Python

The method is an important concept to learn for every programmer or data analyst. We know that in OOP's concept, each object has its attributes and behaviors defined through methods....

3 minutes read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.

How to Install Scikit-Learn

Sklearn or Scikit-learn is a python library used for machine learning. It contains many features like classification, regression, clustering, and Dimensionality reduction algorithms. Sklearn is used to build machine learning...

3 minutes read.

Python oct() function

Python oct() function The oct() function in Python converts an integer number to an octal string prefixed with “0o”. Syntax oct(x) Parameter x: This parameter represents an Integer Number Return This function returns an octal string. Example 1 #...

1 minute read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

Python issubclass() Function

Python issubclass() Function The issubclass() function in Python returns a boolean value ‘True’ if the given object is a subclass of classinfo, else it returns False. Syntax issubclass(class, classinfo) Parameter class: It is a required parameter which represents a tuple...

1 minute read.