Fibonacci Series in C with Example

Fibonacci series is a series of nos.(0,1,1,2,3,5,8………so on) in which a new number is found by adding(+) up to 2 nos. before it for example 0+1=1,   1+1=2,   2+1=3,  3+2=5, 5+3=8…….)

Algorithm for a Fibonacci series  program

Step 1: Start

Step 2: Assign first<-0 and second<-1

Step 3: Print first,second

Step 4:Counter<-2;

Step 5: while(counter<n)

                Third<-first+second

                Print third

                Counter<-counter+1;

                Firt<-second;

                Second<-third;

                End while

Step 6:Exit

//PROGRAM IN C TO PRINT THE FIBONACCI SERIES

#include<stdio.h>

#include<conio.h>

void main()

   {

                int fir,sec,thir,no,countr;

                clrscr();

                countr=2;

                fir=0;

                sec=1;

                printf(“How many nos. u want to generate.\n”);

                scanf(“%d”,&no);

                if(no==0)

                printf(“Sorry!Not available”);

                else if(no==1)

                printf(“%d”,fir);

                else if(no==2)

                printf(“%d%d”,fir,sec);

                else

                 {

                   printf(“%d%d”,fir,sec);

                   while(countr<no)

                    {

                     thir=fir+sec;

                     printf(“\n%d”,thir);

                     countr=countr+1;

                     fir=sec;

                     sec=thir;

                    }

                 }

       getch();

      }      }

Fibonacci series

C Program to display Fibonacci sequence

#include<stdio.h>

#include<conio.h>

void main()

   {

                long fir,sec,sum,final_sum;

                int i,n;

                fir=0;

                sec=1;

                printf(“Pls Enter th value of final_sum for fibonacci”);

                scanf(“%ld”,&final_sum);

                sum=fir+sec;

                while(sum<=final_sum)

                  {

                    sum=fir+sec;

                    printf(“%ld”,sum);

                    fir=sec;

                    sec=sum;

                    printf(“\n”);

                  }

         getch();

    }

Compile: Alt+f9

Ctr+f9

Fibonacci number

Program for Fibonacci Number

#include <stdio.h>

#include<conio.h>

void generateFibonacciSeries(int n) {

    int first = 0, second = 1, next, i;

    printf(“Fibonacci Series up to %d terms:\n”, n);

    printf(“%d “, first);

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

        printf(“%d “, second);

        next = first + second;

        first = second;

        second = next;

    }

}

int main() {

    int numTerms;

clrscr();

    printf(“Enter the number of terms: “);

    scanf(“%d”, &numTerms);

    generateFibonacciSeries(numTerms);

    return 0;

}

Fibonacci Sequence

The generateFibonacciSeries function takes an integer n as an argument, representing the number of terms in the series. It initializes the first two terms as 0 and 1, respectively. It then uses a for loop to calculate and print the next term in the series by summing the previous two terms. The loop continues until i reach n.

In the main function, users are prompted to enter the number of terms they want in the Fibonacci series. The numTerms variable stores the input, and then the generateFibonacciSeries function is called with numTerms as the argument.

When the program is executed, it will print the Fibonacci series up to the specified number of terms. For example, if the user enters 10, the output will be:

Enter the number of terms: 10

Fibonacci Series up to 10 terms:

0 1 1 2 3 5 8 13 21 34