Powered By Blogger

Wednesday 31 March 2021

How to Print Fibonacci Series in 'C' Programming

Welcome viewers to my new blog. Today we will see how to get FIBONACCI SERIES using ‘C’ programming.







It is very easy programming to print ‘Fibonacci Series’ using ‘C’ programming.

First, let's see what is ‘Fibonacci Series’ ?


‘Fibonacci Series’ is a series of numbers in the way that each number is the sum of two preceding numbers.

For example:  a series-  0,1,1,2,3,5,8,13,21,.............

Its a Fibonacci series as we can see, 

the number ‘1’ comes after adding its two precrding numbers (i.e  0,1,1,......  ;  0+1=1),then the number ‘2’ comes after adding its two precrding numbers (i.e  0,1,1,2,......  ;  1+1=2),then we get the number ‘3’ by the same way(i.e   0,1,1,2,3,........;  1+2=3), 
by that way we get the following numbers-

‘5’:  0,1,1,2,3,5,...........;   2+3=5
‘8’:  0,1,1,2,3,5,8,..............;   3+5=8
‘12’:  0,1,1,2,3,5,8,13,............;   5+8=13

This is how the whole ‘Fibonacci Series’ proceed.

Now, let's move to our programming part:-



    1)   VARIABLE DECLARATION PART:  In this part, we have taken the following variables ,all of integer type.

int  ab=0c=1n;

Here, we have initialized the value of b=0 and c=1 in the declaration section. Its use will be discussed in the main programming part.

   2)  GETTING INPUT FROM THE USER:  Here is used a printf function and a scanf function to get the input from the user to know upto how many numbers, he/she wants to get the ‘Fibonacci Series’.

printf("Enter the value of 'n' upto which you want to calculate 'Fibonacci Series': ");
scanf("%d",&n);

1 3)  LOOPING AS WELL AS ARGUMENT AND MAIN CODING PART:  The coding part is very important for getting any ‘Fibonacci Series’.

                           
Here, we have used a ‘while loop’ that will iterate until the value of ‘n’(input from the user) becomes zero(0).

Now, we have given an argument as follows and this is the most important part of that programming:
                                    printf("\n\nThe 'Fibonacci Series' is as follows :-\n");
                                    while(n>0)
                                    {
                                                a=b+c;
                                                c=b;
                                                b=a;
                                                printf("%4d",c);
                                                n--;
                                      }


We have taken the arguments:-   a=b+c;  c=b  and  b=a;
In the initialization part, the value of ‘b=0’ and ‘c=1’.

Let's see the whole loop goes on:-

Let, the user given the value of the variable ‘n’=8

                                 i.            When  n=8 ,b=0 , c=1
a=b+c=0+1=1   So, a=1
Now,  Next Argument:-
c=b , current value of b=0,  So, c=0
b=a ,current value of a=1,  So,b=1

Output:-    0

                               ii.            Now,  n=7(there’s an decrement operator present  i.e  n--) , b=1 , c=0
a=b+c=1+0=1   So, a=1
Now,  Next Argument:-
c=b , current value of b=1,  So, c=1
b=a ,current value of a=1,  So,b=1

Output:-    0  1

                              iii.            Now,  n=6 , b=1 , c=1
a=b+c=1+1=1   So, a=2
Now,  Next Argument:-
c=b , current value of b=1,  So, c=1
b=a ,current value of a=2,  So,b=2

Output:-    0  1  1

                             iv.            Now,  n=5 , b=2 , c=1
a=b+c=2+1=1   So, a=3
Now,  Next Argument:-
c=b , current value of b=2,  So, c=2
b=a ,current value of a=3,  So,b=3

Output:-    0  1  1  2

                               v.            Now,  n=4 , b=3 , c=2
a=b+c=3+2=5   So, a=5
Now,  Next Argument:-
c=b , current value of b=3,  So, c=3
b=a ,current value of a=2,  So,b=2

Output:-    0  1  1  2  3

...........................................

By this way, the whole process goes on and the final output screen becomes :
0   1   1   2   3   5   8  13

  Ø    The program’s argument section can be written in another way:-
printf("\n\nThe 'Fibonacci Series' is as follows :-\n");
                                    while(n>0)
                                    {
                                                a=b+c;
                                                b=c;
                                                c=a;
                                                printf("%4d",c);
                                                n--;
                                      }

  Ø  For this type, we need to initialize the value of  b=1  and  c=0.

  Ø  Trick to remember the code:  You first write blindly the argument,  a=b+c

then, Either you take  b=c  or  c=b
·         If you take  b=c,  then the next argument should be  c=a

Mind by this arrow way:   b=c
                                          
                                         c=a    ⇒ ‘a’ always be in this position

You need the initialize the value of  c=0(on which arrow mark is placed)  and  b=1

·         If you take c=b,  then the next argument should be  b=a
Mind by this arrow way:   c=b
                                           
                                         b=a     ⇒ ‘a’ always be in this position
                                                         
You need the initialize the value of  c=1 and  b=0(on which arrow mark is placed) 


You can remember the series in that way. Hope this helps you. Please share and comment. Thank you for reading.





1 comment:

  1. High Technologies Solutions,
    this the No. 1 institute in delhi for c++ training
    this institute provide you the best knowledge of c++ coures
    here you will get pratical knowledge of c++
    100% job placements from here
    For more details call us on 93110002620
    and visit the websites for more details -https://www.htsindia.com/Courses/modular-courses/c-plus-plus-training-course

    ReplyDelete

Can we use pointers without specifying the data type of the pointer?

Void Pointer Yes, we can use pointers without specifying the data type of the pointers. But remember at the end of the programming, wh...