Factorial program in python using function

Factorial program in python using function : we have provided Factorial program using math. factorial , using the recursion and without using the recursion function. The factorial is the non negative integer that product of all positive integers which is less than or equal to the number for the factorial. The factorial is denoted by the exclamation sign as the (!). The rule of the factorial number 0 is always 1. The factorial of any numbers us the product of all the integers from the 1 to that given number. The example as factorial of 6 is the 1*2*3*4*5*6=720. The factorial does not exist of the negative numbers and the factorial of 0 number is 1.
Example:-
The factorial of the 4 number is 24.
Example:-
num=int (input (“Enter a number :”))
factorial=1
if num<0:
print(“Sorry, factorial does not exist for the negative numbers”)
elif num==0:
print(“The factorial of 0 is 1”)
else
for i in range (1, num+1):
factorial=factorial*i
print(“The factorial of”, num,”is”, factorial)
Output:-
Sorry, factorial does not exist for the negative numbers

Factorial using recursion in python :-

The factorial can be removed by changing the value of number. And the number is stored in num.
Then it is passed to the recur_factorial function to compute the factorial of number.

Example:-

def recur_factorial (n):
if n ==1:
return n
else:
return n*recur_factorial (n-1)
num=7
if num<0:
print(“Sorry, factorial does not exist for negative numbers”)
elif num==0:
print(“The factorial of 0 is 1”)
else:
print (“The factorial of”, num,”is”, recur_factorial (num))

Output:-
The factorial of 7 is 5040
Problem:-
It will take number and find the factorial of number without using the recursion function.

Steps of program execution:-

  1. Take the number from user
    then initialize the factorial of the variable to 1.
  2. Use the while loop to multiply the number to factorial and then decrement the number
  3. You have to continue the step until the number is greater than 0
  4. Print the factorial of number
  5. Then the last step is exit from the program.

Factorial program in python without recursion :-

The source code in the python programming language to find the factorial without using the recursion function.
Program:-
n=int (input (“Enter number :”))
fact=1
while(n>0):
fact=fact*n
n=n-1
print(“Factorial of number is: “)
print(fact)

Explanation:-

  1. The user must enter the number
  2. The factorial is initialized to 1
  3. While loop is used to multiply number to the factorial and then number is decremented each time.
  4. The process will continue till value of number is greater than 0
  5. The factorial of number is then printed

Example:-
Enter number: 5
Factorial of the number is: 120

Naive method:-

n=23
fact=1
for i in range (1, n+1)\
fact=fact*i
print(“The factorial of 23 is:” end=””)
print(fact)
Output:-
The factorial of 23 is: 2585

Using math. factorial :-

The method is defined in the “Math” module because it has the internal implementation.
Math.factorial(x)
Parameters:-
X:-The number has factorial to compute
Return value:-
The factorials will return a desired number

  1. Exception:-

The error is occurred or raised if number is negative or non integral.
Example:-
import math
print(“The factorial of 23 is:” end=””)
print(math.factorial (23))
Output:-
The factorial of 23 is: 25852

a.Exception in the math. factorial:-

If number is negative:-

importmath
print(“The factorial of -5 is:” end=””)
print(math.factorial (-5))
Output:-
                      The factorial of -5 is: 
                      Runtime error:
b.Non-intergral value:-
                      import math
print(“The factorial of 5.6 is:” end=””)
print(math.factorial (5.6))
Output:-

The factorial of 5.6 is:
Runtime error:

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India