# C Program: Fibonacci Series

by Dinesh Thakur Category: C Programming (Pratical)

Let us denote the i th term in the Fibonacci series as Fi , where. i is assumed to take values starting form 0. Thus, the first four terms in the Fibonacci series are denoted as F0, F1, F2. and F3. The first two terms are given as F0=O and F1 = 1.

Each subsequent term is obtained by adding two previous terms in the series. Thus, the first 10 terms in the series are given as 0, 1, 1, 2, 3, 5, 8, 13, 21 and 34. The equation for term Fi , i >2 can be expressed in the form of a recurrence relationship as Fi = Fi-1+ Fi-2.

Let us use the variable term to represent term Fi inthe Fibonacci series and variables prevl and prev2 to represent previous terms (Fi-1and Fi-2) as shown in prev2 prevl term for i =2. Now tenn F2. can be obtained as

term = prevl + prev2;

To determine the next term in the series, first moveprevl and prev2 to next position as shown in using the following statements:

prev2 = prevl;

prevl = term;

Now term F3 can be calculated using the same statement given above (term= prevl + prev2). We can determine the subsequent terms in the series by repeating the above procedure. A program to determine and print the first n terms in the Fibonacci series is given below.

#include <stdio.h>

int old_number;

int current_number;

int next_number;

int main()

{

old_number = 1;

current_number = 1;

clrscr();

printf("Fibonacci Series are : \n");

printf("1 ");

while (current_number < 100)

{

printf(" %d  ", current_number);

next_number = current_number + old_number;

old_number = current_number;

current_number = next_number;

}

getch();

return (0);

} Fibonacci series in c using for loop

/* Fibonacci Series c language */

#include<stdio.h>

void main()

{

int n, first = 0, second = 1, next, c;

clrscr();

printf("Enter the Number of Terms : ");

scanf("%d",&n);

printf("First %d terms of Fibonacci Series are : ",n);

for (c = 0;c < n;c++)

{

if ( c <= 1 )

next = c;

else

{

next = first + second;

first = second;

second = next;

}

printf(" %d ",next);

}

getch();

}

Fibonacci Series Using Recursion

#include<stdio.h>

int fib(int m);

void main()

{

int i,n;

clrscr();

printf("\n Enter no: of Terms = ");

scanf("%d",& n);

for(i=1;i<=n;i++)

{

printf(" %d ",fib(i));

}

getch();

}

int fib(int m)

{

if(m==1||m==2)

{

return(1);

}

else

{

return(fib(m-1)+fib(m-2));

}

} Fibonacci Series upto N number

#include<stdio.h>

void main()

{

int n,n1,n2,n3,i;

clrscr();

printf("Enter the Number : ");

scanf("%d",&n);

n1=0;

n2=1;

printf("%d %d ",n1,n2);

for(i=1;i<=n;i++)

{

n3=n1+n2;

printf(" %d ",n3);

n1=n2;

n2=n3;

}

getch();

} Related Articles on C Programming Dinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.