SHARE

Fibonacci series in C

Fibonacci series are 0 , 1 , 1 ,  2 , 3 ,  5 , 8 , 13 and goes on.

So what is Fibonacci series? It is a series which adds previous two number to get the third number. Let say 0, 1 then the third number is 1; 

0 , 1 = 1

0, 1 , 1 =2

0, 1, 1, 2  = 3(It only adds previous two numbers)

and it goes on.

Explanation:

The logic behind this is very simple

first a = 0;

second b = 1;

now to get c = a+b;

now to run it for n number of time we have to put specific code into the loop : Let’s consider a=0 b=1

c=a+b; //0+1=1

a=b; //a= 1

b=c; //c=1

Second iteration:

1+1=2 ; (c=a+b)

a=1; (a=b)

b=2; (b=c)

so it will go on.. till N iteration. N can be any value given by user.

Code: 

 

Output:

Enter the number: 5
Fibonacci Series: 0 1 1 2 3 5 8

LEAVE A REPLY

Please enter your comment!
Please enter your name here