C Program to display N terms of Fibonacci series

C Program to display N terms of Fibonacci series

 

In this section we write a C Program of Fibonacci Series. Fibonacci Series is a sequence of numbers such that each number is the sum of its two preceding number starting from 0 and 1.

0, 1, 2, 3, 5, 8, 13, 21, 34, 55, …. 

Initially we take 0 and 1 as a seed number and then keep on adding the preceding two numbers to find the next number in the sequence.

For instance the next number after 55 will be the sum of 34 and 55 so 34 + 55 = 89 the next term is 89. Lets see the

Method 1 :- C Program using iteration process

In this we use iteration/loop process to find the Fibonacci sequence. We can do this using any of the three loop for, while or do while.

/* display fibonacci series: 0 1 1 2 3 5 8 .... n terms */
/*using for loop statement */
#include<stdio.h>
int main()
{
   int prev=0, curr=1, term,n,i;
   printf("Enter how many terms to be display \n");
   scanf("%d",&n);
   printf("\nFibonacci series is: \n\n");
   printf("%5d%5d", prev,curr);
   for(i=3;i<=n;i++)
   {
     term = prev+curr;
     printf("%5d", term);
     prev = curr;
     curr = term;
   }
   return 0;
}

Output:-

Enter how many terms to be display
10

Fibonacci series is:

    0    1    1    2    3    5    8   13   21   34

Method 2 :- C Program using Array

In this we use Arrays to find Fibonacci sequence. we assign the 0 and 1 to first two memory block and then keep on adding the previous two numbers to get the next.


#include<stdio.h>
int main()
{
int n;
printf("Enter the Terms that you want to displayed: ");
scanf("%d",&n);
int arr[n];
arr[0] = 0, arr[1] = 1;
for(int i=2;i<n;i++){
arr[i] = arr[i-1] + arr[i-2];
}
for(int i=0;i<n;i++){
printf("%d ", arr[i]);
}
return 0;
}

Output:-

Enter the Terms that you want to displayed: 10
0 1 1 2 3 5 8 13 21 34

Method 3 :- C Program using Recursion

Here we see the recursion implementation of this program. The function kept calling itself until it meet the base condition given in the code.

Here you can see the fib function is calling itself until it reaches the 1 which is the base condition. Let’s see the code.


#include<stdio.h>
int fib(int);
int main()
{
int n;
printf("Enter the Terms that you want to displayed: ");
scanf("%d",&n);
for(int i=1;i<=n;i++){
printf("%d ", fib(i));
}
return 0;
}

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

Output:-

Enter the Terms that you want to displayed: 10
0 1 1 2 3 5 8 13 21 34

Method 4 :- C Program using Dynamic Memory Allocation

/* display fibonaci series: 0 1 1 2 3 5 8 .... n terms */
/*using function with array using pointer */
#include<stdio.h>
#include<stdlib.h>

void fib(int *p, int size);     /*function prototype declaration*/
void display(int *p, int size);
int main()
{
	int *f,n;

	printf("Enter how many terms to be display \n");
	scanf("%d",&n);
	f=(int*)malloc(sizeof(int)*n);
	fib(f,n); /*function call (f is array name and n is size)*/
	printf("\nFibonacci series is: \n\n");
	display(f,n);  /*function call*/
	return 0;
}
/*function definition*/
void fib(int *p, int size)
{
   int *t=p;

   *p=0;
   *(p+1)=1;
   for(t=p+2;t<p+size;t++)
	 *t = *(t-1) + *(t-2);
}
void display(int *p, int size)
{
   int *t;
   for(t=p;t<p+size;t++)
	 printf("%5d", *t);
}

Output:-

Enter how many terms to be display
10

Fibonacci series is:

    0    1    1    2    3    5    8   13   21   34
Related Articles

Leave a Reply