Python Program to Find Factorial of a Number Using For Loop & Recursion Tutorial For Beginners
848 views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
uh so in this video I will show you a
0:04
very famous program which is asked
0:08
inside interview or competitive
0:10
programming which is called as factorial
0:12
program inside Python i will show you
0:15
both the techniques using uh a for loop
0:18
or the technique of recursion or calling
0:21
the same function again and again to
0:23
calculate the factorial of a number
0:27
so factorial is a
0:31
uh basically we calculate factorial if
0:33
you want to calculate factorial of five
0:36
we say 5 * 4 * 3 * 2 * 1 so this is the
0:41
concept of
0:42
factorial so this is uh I will show both
0:45
the uh codes here for for loop so let's
0:50
suppose I show you first of all the for
0:52
loop so I'm just using this website
0:55
pythonutor.com which actually shows the
0:58
realtime execution of the program so
1:00
this is a great tool where you can
1:03
visualize the source code how it is
1:06
executed so let me first of all write
1:08
the program so we declare a
1:13
function factorial iterative and inside
1:16
this we will receive an argument of
1:18
n so we declared this function so as you
1:22
can see we are receiving this number as
1:24
an argument so now to calculate the
1:27
factorial we need a variable of result i
1:30
will initialize this variable to one and
1:34
then we will put a for loop which will
1:37
run
1:39
from 1 up
1:42
till n
1:45
so for this in Python what this works is
1:49
it will if the value of n is five so it
1:53
will run from 1 to
1:56
uh five so this is how which so the
2:00
initial value of i is equal to 1 and if
2:04
the value of n is let's suppose five so
2:08
it will stop at five itself so this
2:11
value is not ex it it is not included so
2:15
it will run up till 1
2:18
to5 so I will explain you when I uh
2:21
visualize this program using this tool
2:24
so now in the factorial program we
2:26
simply multiply the number so we simply
2:28
say result multiplied by whatever is the
2:32
value of i and then lastly we return the
2:36
result that's
2:38
all and now we simply first of all take
2:42
the number from the
2:45
Using the integer we type cast
2:48
it we use the input function to receive
2:51
the value so here we will simply ask the
2:55
user to enter a number so here the user
2:59
will enter the number and then we will
3:01
print out we will call this
3:07
function and just display the result so
3:10
we are calling this function so if I
3:12
execute uh click this button of
3:14
visualize execution so now step by step
3:17
just see how the code is executing so if
3:20
I show you first of all this function is
3:24
registered in the global
3:27
frame this is your function and then
3:31
here it
3:32
actually ask enter a number so I will
3:35
enter five here and click on submit so
3:39
now the initial value of number is five
3:43
so now this value is passed here if I go
3:45
to the next step so this function is
3:48
defined using this n value is five here
3:52
if you see n value is five and now if I
3:56
go to the next step so it basically
3:59
initializes result to one so as you can
4:02
see result is equal to 1
4:05
and then in the next step we initialize
4:07
this for loop as you can see the initial
4:10
value of I is set to one so the initial
4:13
value of I is equal to 1 and then we
4:16
multiply whatever is the
4:19
result var variable was 1 so 1
4:22
multiplied by 1 comes out to be 1 so we
4:28
come to the next iteration when I
4:30
becomes two so now the i value will
4:33
become two so 2 * 1 because result is
4:37
equal to 1 so which will be comes out to
4:40
be two so as you can see now I becomes
4:43
two here so 2 * 1 so result also becomes
4:48
two so as you can see and now we come to
4:51
the next step now i becomes
4:53
three so in the next iteration I becomes
4:56
three so 3 * 2 so this will become six
5:02
so as you can see now the result value
5:04
becomes six so now in the next iteration
5:06
I will be incremented furthermore I will
5:09
become four so 4 * 6 comes out to be 24
5:16
so as you can
5:17
see now I become 5 the next
5:21
iteration again we multiplied 5
5:24
multiplied by 24 this time so it comes
5:27
out to be 120 and now I become six so
5:31
six is not included here so as soon as
5:34
the program hits this the for loop will
5:36
break and now we will come to this step
5:40
return result so now we come to this and
5:44
now your return value is 120 and that's
5:47
all after that we print out this in the
5:52
next step so now the program ends we
5:54
display the factorial comes out to be
5:56
120 so this is a very easy program in
6:01
this we use the for loop very
6:04
easily and now I will be showing you the
6:06
second approach by which you can achieve
6:09
it the concept of recursion recursion is
6:14
the way by which we actually call the
6:16
same function again and again so let's
6:19
suppose I define this function here
6:24
so this is your recursive function and
6:27
here we will put a end condition as well
6:31
because in recursive function you do
6:33
need to put it this condition because if
6:35
you don't put this condition the program
6:38
will run endless number of time so we do
6:41
need to put this condition that if n
6:43
becomes zero or if n becomes one in
6:46
either of these cases we need to return
6:49
a one and
6:52
just end this but up till now if n is
6:57
not equal to zero or one in that case we
7:00
need to multiply n multiplied by and
7:03
then we need to call the same function
7:06
again here and this time n minus
7:10
one so we are following the same
7:13
technique here if you want to calculate
7:15
the factorial of five so we simply say 5
7:20
* again we call the same function so 5 -
7:24
4 so like this so 5 4 * 3 so this is the
7:30
same technique that we are using and
7:32
again let me show you how it executes
7:35
step by step
7:38
so again we simply take the user input
7:41
using the input function and then we
7:43
print out the factorial so now if I
7:45
click the visualize execution so now
7:48
what happens right here if you see first
7:51
of all the function is
7:56
registered in the global frame here the
7:58
function is registered now we need to
8:02
say again if I print out number here we
8:06
need to submit so now
8:08
the function is initialized here you can
8:11
see factorial num is five if I go to the
8:15
next step the function is registered
8:17
with n is equal to 5 and then I come to
8:21
the next step this if condition so the n
8:25
value is five here so it is not equal to
8:27
zero so this will fail here and then we
8:31
come to this step so 5
8:33
* and this value will be four 5 - 1 so
8:38
now the n value become four here as you
8:41
can
8:41
see so this stays here so 5 multiplied
8:45
by and we are again calling the same
8:47
function but this time it is four and
8:51
then we come to this again this
8:53
condition will fail and
8:55
now n value is four so four multiplied
8:59
by again we call the same function now
9:01
the value will become three can
9:04
see can see that again we come same
9:07
condition hits now it will become two as
9:11
you can see so these are recursive
9:14
functions guys as you can see these are
9:17
recursive
9:19
functions and we are waiting for the
9:22
condition when be n becomes one so now
9:26
again we check we n is not equal to 1 so
9:30
we again come and this time n will
9:32
become one so now this condition is now
9:36
met here and now again if I check for
9:40
this condition so now n has become one
9:43
so in this case the condition will
9:45
evaluate to true and now we will be
9:47
returning one so now what happens if I
9:50
click next
9:51
again so this return one statement will
9:54
execute and now your return value is
9:57
there so now return values come so 1
10:00
multiplied by again we need to execute
10:04
the previous recursive functions which
10:06
were not executed so 1 multiplied by two
10:09
so this will comes out to be return
10:12
value
10:13
two so this is happening internally as
10:17
the condition has met now so now this
10:19
return value has this function is
10:22
executed now this return value will
10:24
transfer to the next recursive function
10:27
so 2 * 3 comes out to be six so as you
10:31
can see so now 6 * 4 comes out to be 24
10:37
so as you can see now 24 * 5 which is
10:42
120 at last recursive function that's
10:45
all so this is your return value and if
10:48
I click next and now your factorial has
10:50
been printed out so both these
10:52
approaches guys I showed you for loop
10:55
and recursion i personally like this
10:57
program which is recursion because it's
10:59
short concise and also it is following
11:02
the concept of recursion which is a good
11:05
programming concept in which you execute
11:07
the same function again and again to
11:11
basically break the problem into
11:13
multiple parts and then execute this so
11:16
these are the two programs which are
11:17
asked come specifically in interviews
11:20
whenever you go for interview or
11:22
competitive programming and thank you
11:24
very much guys for watching this video
11:27
also check out my website
11:28
freemediattools.com
11:30
uh which contains thousands of tools
#Programming
#Scripting Languages