C program for Fibonacci Series | C code Champ

# C program for Fibonacci Series | C code Champ

2979
2
SHARE

C program for Fibonacci series using recursion  and using FOR loop : Fibonacci series is a mathematical series of in which next number of series is derived by sum of previous two number and 0 & 1 are first two numbers of the series. So Lets see how the Fibonacci series logic works:

0,1,0+1 =1, 1+1=2, 2+1 =3, 3+2 =5, 5+3 =8, and so on..

If we wish to generalize this concept then it would be something like :

xn = xn-1 + xn-2

where:

• xn is term number “n”
• xn-1 is the previous term (n-1)
• xn-2 is the term before that (n-2)

Now when we write we need to keep first two numbers into consideration that is 0  and 1 (i.e. these need to hard coded in program). And then from then onwards our above generalized logic will work for us. So friends let see how to write .

### C program for Fibonacci series using recursion :

```#include<stdio.h>

int Fibonacci(int);

main()
{
int n, i = 0, c;

scanf("%d",&n);

printf("Fibonacci series\n");

for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}

return 0;
}

int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}```

We can also code the same Fibonacci series logic using different method say by using the FOR loop. Let see how to write the of Fibonacci series using For loop.

### C program of Fibonacci series using For Loop or without recursion:

```#include<stdio.h>

main()
{
int n, first = 0, second = 1, next, c;

printf("Enter the number of terms\n");
scanf("%d",&n);

printf("First %d terms of Fibonacci series are :-\n",n);

for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}

return 0;
}```

I hope you all have enjoyed the article. If you face any issues with source code let us know in form of comments.

SHARE
Previous articleHow to install Turbo C in Windows 7 using Dosbox
Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games. Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com