Python Program to Find Factorial of a Number

In this python tutorial, you will learn how to to Find Factorial of a Number using the if, elif and else statements along with the range function and For loop of the python programming language.

How to Find Factorial of a Number?

Let’s take a look at the source code , here the values are given as input by the user in the code, the if, elif and else statements along with the for loop carry out the function.

RUN CODE SNIPPET
# To take input from the user
num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero
if num < 0:
   print("\nThe factorial for negative numbers does not exist ")
elif num == 0:
   print("\nThe factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("\nThe factorial of",num,"is",factorial)

INPUT:

5

OUTPUT:

Enter a number: 
The factorial of 5 is 120
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable num  with the statements/strings ("Enter a number: ") , we use the int function and declare the  input value as an integer value.
  2. We declare the variable factorial with the integer  1.
  3. In the STDIN section of the code editor the input values are entered.
  4. We declare the if statement with the condition num < 0  followed by a :and if the condition is satisfied the print function will display the statement ("\nThe factorial for negative numbers does not exist"). If not,  it moves to the next step which is the elif statement.
  5. We declare the elif statement with the condition num == 0  followed by a :and if the condition is satisfied the print function will display the statement ("\nThe factorial for 0 is 1"). If not,  it moves to the next step which is the else statement.
  6. We declare the else statement with a for loop and the variable i  along with the range function which will hold the values of i between the range (1,num + 1) , followed by a : .
  7. We declare the variable factorial with the condition factorial*i, this condition will iterate until the final value in the range is used.
  8. Once the iteration is done, the obtained value is stored in the variable factorial  and the print function will display the statement ("\nThe factorial of",num,"is",factorial).

NOTE:

  • The input() function allows a user to insert a value into a program, it returns a string value.
  • The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
  • The elif is short for else if, it allows us to check for multiple expressions.
  • A For loop runs a block of code until the loop has iterated over every item in a condition, that is it helps to reduce any repetition in the code because it executes the same operation multiple times.
  • The Range() function is used to generate a sequence of numbers, where the range() function is commonly used in for looping functions.
  •  The colon : at the end of the if, elif and else statement tells Python that the next line of code should only be run if the condition is true.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.
  • The print statement/string to be displayed in enclosed in double quotes.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else...
In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...