Home

A program to print FIBONACCI series using RECURSION

C Programming Language 086

Program

#include <stdio.h>

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

void main()
{
    int i, p, o;
    printf("Enter value of p: ");
    scanf("%d", &p);

    for (i = 1; i <= p; i++)
    {
        o = fib(i);
        printf("%3d", o);
    }

    printf("\n");
}

Result

Enter value of p: 5
  1  1  2  3  5


Last Updated on

Next Post: Rules vs Guidelines →

Comments