Tuesday, February 10, 2015

C program to display fibonacci series


The c program given below is able to display the Fibonacci series. Fibonacci series is a series of numbers where the third number = first number + second number.

For example: 0, 1, 2, 3... is a series of all positive integer numbers.

But a Fibonacci series will be like 0, 1, 1, 2, 3, 5, 8, 13, 21 ... where a number is a summation of previous two numbers.

After running the program, you will be asked to provide the limit. That means how many numbers of the series you want to display. For example: if you provide 5 as the limit, then 0, 1, 1, 2, 3 will be displayed from the series.

C codes to display fibnacci series is given below...

//C program to display fibonacci series
#include<stdio.h>

void main(){
//declaring integer type variables
int limit, i, first = 0, second = 1, next=0;

//displaying message to enter the limit
printf("Enter the limit: ");

//storing the limit into a variable
scanf("%d",&limit);

//for-loop to display the fibonacci series
for(i=0; i<limit; i++){
   if(i<1){
       next = i;
   } else {
       first = second;
         second = next;
       next = first + second;
   }

   //displayong the fibonacci series
   printf("%d, ",next);
}

}

Lets see how the above codes work!
Remember all the lines started with double slash (//) will not be executed by the compiler. They will be considered as comments.

Including header file
The line #include<stdio.h> is used to add the header file 'standard input output'. std if for standard, i is for input and o is for output & finally stdio.h goes for standard input output header file.

Starting main function
From the line void main(){ the main function will be started. Sometimes it may be it main().

Declaring integer type variables
In the line int limit, i, first=0, second=1, next=0; , I have declared five integer type variables limit, i, first, second & next. Some of them are initialized also.

Displaying message to user
printf("Enter the limit: "); is used to display a message, that's why the user can realize what to do.

Getting input from keyboard
The line scanf("%d",&limit); will get integer type data from keyboard and will store that into the variable limit.

for-loop to create and to display fibonacci series
The for-loop has started with the line for(i=0; i<limit; i++){. In the for-loop i is initialized to zero (0) and the loop will be continued till the value of i is less than the value of limit. So, if you provide the limit 5 - then the loop will run 5 times when the value of i is 0, 1, 2, 3 & 4. In every loop the value of i will increase by 1 as i++ is given there.

Let consider a real example, so that you can understand the above program more deeply.

» Step 1
When you will run the program then a message will be displayed like...
Enter the limit:

for example if you provide the limit 7, then 7 will be stored into the variable limit by the line scanf("%d",&limit);

Now in the for-loop, i is initialized to 0 what is less than the value of limit. So the for-loop will run. The value of i will be increased by 1 and the if-else condition will be executed lied inside the for-loop.

if(i<1) condition is true, so next=i; operation will be performed and the value from i will be stored into the variable next. In this case the else condition will not be executed and the line printf("%d, ",next); will be executed. As there is 0 in the variable next now, so 0 will be printed with a comma (,).

» Step 2
Again the condition of the for-loop will be checked. As there is 1 in the variable i that is less than than the value of limit (7), so the if condition [if(i<1)] will be checked. In this case the if-condition is false, so the else-condition will be executed.

Inside the else condition there is...
     else {
       first = second;
         second = next;
       next = first + second;
   }

Now we know- first=0, second=1 and next=0
After executing the else condition we will get- first=1, second=0 and next=1+0=1

So, 1 will be printed just after 0, and now on the display there will be 0, 1,

» Step 3
As i=2, so the for-loop will be executed and if condition will be checked again. The if-condition is false, so the else will be executed and we will get...

first=0, second=1 and next=0+1=1

So, another 1 will be printed after 0, 1, and the series will be 0, 1, 1,

» Step 4
Now we have i=3, for-loop will be executed and if-condition will be denied. Again else condition will be executed and we will get...

first=1, second=1, next=1+1=2

So, 2 will be printed after the existing series and the series will be 0, 1, 1, 2,

» Step 5
As i=4 now, so the for-loop will be executed and the if-condition will be denied. So we will get...

first=1, second=2, next=1+2=3

So, 3 will be printed and the series will be 0, 1, 1, 2, 3,

» Step 6
We have i=5 and we will get...

first=2, second=3, next=5

So, 5 will be printed after the series like 0, 1, 1, 2, 3, 5,

» Step 7
We have i=6 and we will get

first=3, second=5, next=3+5=8

so, the series will be 0, 1, 1, 2, 3, 5, 8,

» Step 8
Now we have i=7 and there is 7 inside limit, so the condition for(i=0; i<limit; i++) is false and the loop will not run anymore. Finally, the program will output 0, 1, 1, 2, 3, 5, 8, and will stop.

Advertisement


1 comment:

  1. An interesting program, creative job! Thanks for sharing.

    ReplyDelete