UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to generate Fibonacci series

C program for How to generate Fibonacci series by giving an input of limit.

by Krishna viswambharan


Posted on 05 Sep 2017 Category: C Views: 82

Edited on 21 Sep 2017



C program to generate Fibonacci series

How to display the Fibonacci sequence up to the limit n???

Fibonacci sequence is a series where the next term is the sum of previous two terms. The first two terms of the sequence are 0 followed by 1.

#include<stdio.h>
#include<conio.h>
void main()
{
       int i,n,f1=-1,f2=1,f3=0;
       printf(“Enter the limit :”);
       scanf(“%d”,&n); // getting input from the user
       printf(“Fibonacci series upto the limit %d are :\n”,n);
       for(i=1;i<=n;i++)
       {
           f3=f1+f2;  //f3 is the next number and next number in the sequence is the sum of previous two terms ie, f1 and f2
           printf(“%d\t”,f3);
           f1=f2;  //first=second
           f2=f3;  //second=next
       }
       getch();
}

In the first iteration f3=0(-1+1)

                                         f1=1

                                         f2=0

In the second iteration f3=1(1+0)

                                          f1=1

                                          f2=1

 This iteration process continuous until the condition become false. 

output



Leave a Comment:


Advertisement