Calculations

Python program to find factorial of a number

Python program to find factorial of a number

In this tutorial, we will discuss Python program to find factorial of a number.

In this post, we use if statements and for loop to calculating factorial of a number

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

Factorial of n is

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

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

 

Here, we can follow some procedure to reach the completion of this program

  • Take a number of input from the user
  • Initialize a factorial variable to 1
  • A for loop is used to multiply the number to find factirial (for the continuing process)
  • This process continue until the value of the number is greater than zero
  • The factorial of the given number is printed

Program 1

#python program to find factorial of a number
#the user can provide input for calculating factorial as they wish
num=int(input("Enter a number to find factorial: "));
factorial=1; #declare and initialize variable for factorial

#check if the number is negative or positive or zero
#using if statement in Pyton
if num<0:
   print("factorial does not available for a negative number");
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);

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

Case 1

Enter a number to find factorial: -2
factorial does not available for a negative number

Factorial is not defined for the negative integer value

Case 2

Enter a number to find factorial: 0
The factorial of 0 is 1

Factorial of zero is  one

Case 3

Enter a number to find factorial: 5
The factorial of 5 is 120

 

In the above program, we can check the given number negative, zero or positive using if elif else statements in python

if the number is negative, the output is displayed as follows “factorial does not available for a negative number”

if the number is zero, the output is displayed as follows “The factorial of 0 is 1”

otherwise, if the number is positive, the range function calculates factorial

 

Similar post

Java program to find factorial of a number 

C program to find factorial of a number

C++ program to find factorial of a number

Java program to find factorial of a number using recursion

C program to find factorial of a number using recursion

C++ program to find factorial of a number using recursion

 

Suggested for you

Operator in Java language

Data type in Java language

For loop in Java language

While loop in Java language

Nested for loop in Java language

Nested while loop in Java language

 

Operator in C++ language

Data type in C++ language

For loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

 

Operator in C language

For loop in C language

While loop in C language

Nested for loop in C language

Nested while loop in C language

 

 

 

C program to find factorial of a number
Java code to find factorial using method
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.

Recent Posts

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 hour ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

3 days ago

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