C program for Fibonacci series using recursion

1. C code to print Fibonacci series by recursion
2. Fibonacci series in c by using recursion

3. C code for Fibonacci series using recursion
4. Program to generate Fibonacci series using recursion in c

#include<stdio.h>

void printFibonacci(int);

int main(){

    int k,n;
    long int i=0,j=1,f;

    printf("Enter the range of the Fibonacci series: ");
    scanf("%d",&n);

    printf("Fibonacci Series: ");
    printf("%d %d ",0,1);
    printFibonacci(n);

    return 0;
}

void printFibonacci(int n){

    static long int first=0,second=1,sum;

    if(n>0){
         sum = first + second;
         first = second;
         second = sum;
         printf("%ld ",sum);
         printFibonacci(n-1);
    }

}

Sample output:

Enter the range of the Fibonacci series: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89

C code to print Fibonacci series without recursion:

#include<stdio.h>

void printFibonacci(int);

int main(){

    int k,n;
    long int i=0,j=1,f;

    printf("Enter the range of the Fibonacci series: ");
    scanf("%d %d ",&n);

    printf("Fibonacci Series: ");
    printf("%d ",0);
    printFibonacci(n);

    return 0;
}

void printFibonacci(int n){

    long int first=0,second=1,sum;

    while(n>0){
         sum = first + second;
         first = second;
         second = sum;
         printf("%ld ",sum);
         n--;
    }
}





Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples

25 comments:

akshay76 said...

void main()
{
int a,b,c,i,n;
a=0;
b=1;
printf("Enter number of terms of the series to be generated:");
scanf("%d",&n);
if(n==1)
{
printf("\n FIBONACCI SERIES\n");
printf("%d",a);

}
else if(n==2)
{
printf("\n FIBONACCI SERIES\n");
printf("%d\t%d",a,b);

}
else
{
printf("%d\t%d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);

}
}
getch();
}

OR

void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
printf("\t%d\t%d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}

SURYAKANT said...

Find non fibonesis serize using function

CSE Answer said...

#include
#include
Void main( )
{
Int n, i, c, a = 0, b = 1;

printf(“Enter Fibonacci series of n’th term”);
scanf(“%d”, & n);
printf(“%d%d”, a, b);
for ( i = 0; I <= (n-3); i++)
{
c = a + b
a = b
b = c
printf(“%d”,c);
}
getch( );
}

Out put:
Enter the Fibonacci series of n’th term : 7
0112358

dlovan bashir ali said...

//in c++
//this is short code of Fibonacci function in recursion..

int fib(int n){

if(n==0){
return 0;
}else if(n==1){
return 1;
}else{

return fib(n-1)+fib(n-2);
}
}

Void main( )
{

cout<<fib(n)<<endl;

}

CProgrammer said...

How can I tell if a given source code has used recursion or not, when there is a function declared?

Anonymous said...

This is really helpful.
For Dynamic Programming this seems 2 be d brief one...

Unknown said...

#include

void printFibonacci(int);

int main(){

int k,n;
long int i=0,j=1,f;

printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);

printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n);

return 0;
}

void printFibonacci(int n){

static long int first=0,second=1,sum;

if(n>0){ // there is error it should be if(n>2)since we have already printed two digit 0 1
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}

}

Unknown said...

plz..
Check the logic is wrong

Ahmed Hamdy said...

There is a better solution for fibo recursive:

Notes:
Call it with n > 0 and 0, 1 as initial fibo elements.
Print a 0 before the call.

The code:
void fibo_recursive(int n, int i, int j)
{
if (n > 0)
{
printf("%d, ", j);
int temp = j;
j = j + i;
i = temp;
fibo_recursive(--n, i, j);
}
}

Unknown said...

its not working

Unknown said...

Make it n-2 in for statement

Unknown said...

thankssssss

Anonymous said...
This comment has been removed by the author.
Unknown said...

#include

void printFibonacci(int);

int main(){

int k,n;
long int i=0,j=1,f;

printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);

printf("Fibonacci Series: ");

printFibonacci(n);

return 0;
}

void printFibonacci(int n){

static long int first=0,second=1,sum;
if(n==0)
return;
if(n==1)
printf("%d",first);
else
printf("%d",first);
sum=first+second;
first=second;
second=sum;
printFibonacci(n-1);

}

Unknown said...

#include

void printFibonacci(int);

int main(){

int k,n;
long int i=0,j=1,f;

printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);

printf("Fibonacci Series: ");

printFibonacci(n);

return 0;
}

void printFibonacci(int n){

static long int first=0,second=1,sum;
if(n==0)
return;
if(n==1)
printf("%d",first);
else
printf("%d",first);
sum=first+second;
first=second;
second=sum;
printFibonacci(n-1);

}

Unknown said...

no the logic seems to work perfectly

Touhid said...
This comment has been removed by the author.
Noman Ibne Mimdad Turzo said...

#include

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

int main()
{
int number,i=0;
printf("Enter Number for fib series : ");
scanf("%d",&number);
while(i<number)
{
printf("%d ",fib(i));
i++;
}


return 0 ;
}

hb74 said...

#include

void main()
{
int i,n;
int fab(int);

printf("Enter numbers of terms of Fabonnacci series :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d, ",fab(i));
}
printf("\n");

}

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

Social Issue said...

what is the meaning of long int or static long in recursion program

sanket said...

what is program & why using program

Unknown said...

write a program 1 2 2 4 8 32 series like that display

Unknown said...

the declaration of variables first,second and sum should be made global to get correct output

Unknown said...

I don't like to print 0 and 1 manually....so try this...
void printFibonacci(int n){

static long int first=-1,second=1,sum;

if(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}

}

Jenifer said...

int fibo(int n)
{
if(n<=1)
return n;
else
return (fibo(n-1)+fibo(n-2));
}
main()
{
n=6;
for(i=0;i<n;i++)
printf("%d",fibo(i));
}