Find elements

Find Factorial:Python program to using function

Python program to find factorial using function

In this tutorial, we will discuss the Python program to find factorial using function

In this program, we are going to learn about how to find  factorial using  the function in Python language

Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one

Python program to find factorial using function

 

  • Factorial is not defined for negative numbers
  • Factorial of zero(0) is: 1
  • Factorial of a positive number is given below

Example

factorial of 5 is

5!=5*4*3*2*1=120

 

factorial of 7 is

7!=7*6*5*4*3*2*1=5040

 

The factorial of a positive number n is given below

factorial of n is

n!=n*(n-1)*....2*1

In my previous post, I have explained the various ways to Find factorial of a number in Python

However, in this program, we embed the logic of the factorial program in the function

you can embed the logic of the factorial program to find the factorial of numbers using any of the following approaches in the function

The user can provide numbers as they wish and get the factorial according to their input

Program 1

Find factorial using Python function – using for loop

#Pyton program to find factorial of a number

def factorial(num):#function definition
    fact=1

    for i in range(1, num+1):#for loop for finding factorial
        fact=fact*i
    return fact    #return factorial 

number=int(input("Please enter any number to find factorial: "))

result=factorial(number)#function call and assign the value to variable result

print("The factorial of %d = %d"%(number,result))

 

When the above code is executed, it produces the following results

Please enter any number to find factorial: 6
The factorial of 6 = 720

 

Find factorial using Python function – using while loop

def factorial(num):#function definition
    
    fact=1;
    i=1
    while i<=num:
        fact=fact*i
        i=i+1
    return fact
    
num=input("Enter a number..");

result=factorial(num)

print("factorial of the number: %d" %result)

factorial(num)#function call

When the above code is executed, it produces the following results

Enter a number..5
factorial of the number: 120

 

Programming approaches for implementing these program

  • Take a number as input  from the user and stored in variable number for finding factorial
  • Declare a python function to find factorial
  • Inside the function, declare the variable fact and initialise as 1
  • Inside the function, find the factorial of a given number using for loop in Python
  • Run the loop from given number until 1 and multiply numbers
  • Call the factorial() function and assign the output to variable result
  • the factorial of the given number is displayed using the print() function in Python

 

Check out these related examples

Java code to calculate the factorial of a number

Java code to calculate the factorial of a number using Java method

Calculate factorial of a number in C

Calculate factorial of a number in CPP

Calculate factorial of a number in Python

Calculate  factorial of a number using while loop in Python

 

Suggested for you

Operator in Python

Function in Python

For loop in Python

 

Python program to check whether a number is even or odd
Python program find factorial of a number using recursion
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • Fantastic site you have here but I was curious about if you knew of any user discussion forums that cover the same topics talked about here?
    I'd really like to be a part of online community where I can get suggestions from other knowledgeable individuals that share the same interest.
    If you have any suggestions, please let me know. Thanks!

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago