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
- 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 variablenum
with the statements/strings("Enter a number: ")
, we use theint
function and declare the input value as an integer value. - We declare the variable
factorial
with the integer1
. - In the STDIN section of the code editor the input values are entered.
- We declare the
if
statement with the conditionnum < 0
followed by a:
and if the condition is satisfied theprint
function will display the statement("\nThe factorial for negative numbers does not exist")
. If not, it moves to the next step which is theelif
statement. - We declare the
elif
statement with the conditionnum == 0
followed by a:
and if the condition is satisfied theprint
function will display the statement("\nThe factorial for 0 is 1")
. If not, it moves to the next step which is theelse
statement. - We declare the
else
statement with afor
loop and the variablei
along with therange
function which will hold the values ofi
between the range(1,num + 1)
, followed by a:
. - We declare the variable
factorial
with the conditionfactorial*i
, this condition will iterate until the final value in the range is used. - Once the iteration is done, the obtained value is stored in the variable
factorial
and theprint
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.