Here you will find a variety of videos on technology, Tutorials, News, Tip And Tricks, Reviews

Breaking

Post Top Ad

Your Ad Spot

Wednesday, August 24, 2016

Write a C program to print fibonacci series using recursion and without using recursion.

Fibonacci series Program in C


Fibonacci series of n is next number is sum of previous two numbers for example ex: 0  1 1 2 3 5 8 13 21 etc. The first two number of fibonacci series are 0 and 1.




Fibonacci series program without recursion.


#include<stdio.h>
void main()
{
    int n1=0,n2=1,n3,i,num;
    printf("Enter the number");
    scanf("%d", number);
    printf("\n %d %d",n1,n2);
    for(i=2;i<num;i++)
    {
         n3=n1+n2;
         printf(" %d",n3);
         n1=n2;
         n2=n3;
    }
}

Output:




Download

Fibonacci series program without recursion.


#include<stdio.h>
#include<conio.h>
void fibonacci(int n)
{
    static int n1=0,n2=1,n3;
    if(n>0)
    {
       n3=n1+n2;
       n1=n2;
       n2=n3;
       printf("%d",n3);
       fibonacci(n-1);
    }
}
void main()
{
    int n;  
    printf("Enter the number");
    scanf("%d",&n);
    printf("%d %d",0,1);
    fibonacci(n-2);
}


Download

Post Top Ad

Your Ad Spot

Pages