Get the full source code of application here:
Show More Show Less View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you a very
0:05
common program which is asked inside
0:06
placements or interview questions for
0:09
GATE uh for competitive exams it's
0:12
called as Fibonacci sequence or pattern
0:15
if you want to print this uh fib fiboni
0:18
sequence or pattern it's a very special
0:20
pattern where uh the next number is
0:25
the sum of the previous two numbers so
0:28
basically this is actually the pattern
0:31
so if I just write let me just open the
0:34
zoom it tool on my machine so that I can
0:37
zoom on particular element so just
0:40
wait so this is essentially this pattern
0:43
if you just write
0:47
01 so as you can see these are the first
0:50
two numbers in the pattern 0 1 and this
0:54
third number is the sum of the previous
0:56
two numbers this is 0 + 1 which comes
1:00
out to be 1 and now the next number
1:02
which will come in the pattern which it
1:04
will be the sum of these two numbers
1:06
which will be 1 + 1 and this comes out
1:10
to be two and now the next number will
1:13
be the sum of these two numbers which
1:15
will be 1 + 2 which will be three and
1:19
now this will be the sum 2 + 3 as you
1:21
can see this is actually the pattern
1:23
which is called as Fibonacci sequence or
1:25
pattern and now we just need to print
1:28
this pattern using a Python program so
1:31
this Python program executes like this
1:33
so if I try to execute this program so
1:36
Python app py it will ask me to the
1:39
enter the number for fibonaki sequence
1:41
so how many sequence you want to print
1:44
out so I will say I want to print the
1:47
pattern up till
1:48
10 or five numbers so it will print out
1:53
the pattern here you can see 01 the next
1:56
number is the sum of this first two
1:59
numbers and the next number is 1 + 1 2
2:03
and then 2 + 1 3 so this is the thing if
2:08
you say 10 you will see again it follows
2:12
the same
2:14
pattern so now I will show you the
2:18
Python program how to build this so the
2:21
very first thing we need to realize that
2:24
in this pattern the first two numbers
2:26
are constant which is 0 1 so
2:29
irrespective of how many sequence I want
2:31
to print out these two numbers are
2:33
printed like this
2:34
01 so let me first of all write this
2:38
program so here we will be accepting the
2:40
user input so for this we will be type
2:43
casting the value here to integer and we
2:46
will ask the question to the user that
2:48
enter the number for
2:52
Fibonacci
2:54
sequence so now the user writes the
2:57
number and right here we will create two
2:59
variables a is equal to zero and b is
3:02
equal to 1 so these two numbers will be
3:04
printed out irrespective of how many
3:09
uh digits you get so these two numbers
3:13
are constant so we just need to print
3:15
these numbers like this so print the
3:18
first number so here we need to print
3:22
like this the first number and the
3:25
second number
3:27
but the problem here is that if I try to
3:29
execute this this appears on next line
3:32
like this 01 but if you want to print
3:36
side by side there is a end function
3:38
that you can use put a comma and then
3:41
you can put a end symbol is equal to
3:44
double quote so this means that it
3:47
will appear both these characters will
3:50
appear side by side if you want some
3:53
space then you can add some space here
3:55
so we can add some space like like
3:59
this so this means that both these
4:01
numbers will appear side by side so in
4:04
this way you can do this so if I try to
4:07
execute this now you will see 01 is
4:09
printed out and now we need to use the
4:12
for loop for printing out the other
4:15
numbers so for this we will use the for
4:18
loop for i in range range so it will run
4:23
from the second index up till the total
4:26
number of numbers that it receives so
4:28
let's suppose I provide five here it
4:31
will run up till
4:34
2 5 five is not included because this
4:38
number is not included so it will run up
4:40
till 2 n minus one so 2 to
4:45
4 so inside this for loop what we need
4:48
to do we need to calculate the sum of
4:52
the these two numbers as you can see 0 +
4:56
1 which will give you the third number
4:59
which will be 1 for this we will be
5:01
using the third variable so we create
5:03
this third variable we calculate the sum
5:06
which will be a + b and then we store
5:09
this sum inside this third variable c
5:12
and whatever is the sum right here and
5:15
for
5:16
this one more thing so if I try to print
5:19
this number like this if I print this
5:22
you will see the problem which will be
5:25
there if I print this C number and if I
5:30
run this now you will see the pattern
5:32
will not be printed out you will see 0
5:36
1 up till now this the pattern is
5:39
correct because 0 + 1 comes out to be
5:42
one but here also the next number is
5:45
also one the next number is also one so
5:47
now we need to make sure that we also
5:50
switch the value of uh a and b because a
5:55
and b is set to zero and one so we need
5:58
to make sure that uh uh the values of a
6:00
and b in the next iteration of when we
6:04
move the index to the sec uh next number
6:08
when we go to the third index this third
6:10
number we need to make
6:13
sure a is set to this number and b is
6:16
set to this for doing this we need to
6:18
swap the index here for doing this we
6:21
simply say a is equal to
6:25
b and b is equal to c so this is a
6:30
swapping that we are doing right here
6:33
using a third variable so we calculate
6:35
the sum and then we make sure that we
6:37
interchange the
6:38
indexes the next the second number
6:41
becomes the first number and then the
6:44
sum of these two numbers becomes the
6:47
second number so now if you run this
6:50
pattern you will see the correct pattern
6:52
will be printed out you will see 0 1 the
6:56
sum of these two numbers comes out to be
6:58
the third number and then we move to the
7:01
next number which is 1 + 1 2 and then 1
7:06
+ 2 3 this is essentially this pattern
7:09
comes out to be and if I let's suppose
7:14
you can see this is 10
7:16
numbers 1 + 1 2 2 + 3 5 5 + 8 13 + 8 21
7:24
+ 13 34 now this is the way by which you
7:27
can print out this Fibonacci sequence or
7:30
pattern this is asked inside various
7:32
competitive exams such as in coding
7:35
placements as well so GATE exams as well
7:39
so in this way you can uh write this
7:42
program in Python and thank you very
7:44
much for watching this video please hit
7:46
that like button subscribe the channel
7:48
and also check out my website
7:51
freemediattools.com
7:53
uh which contains thousands of tools
