20_10

Write a C program which will generate Fibonacci sequence using recursive function printFibonacci(int n).

 Source code:

/*A recursive procedure or routine is one that has the ability to call itself. Consider a C program which will generate Fibonacci sequence using function printFibonacci(int n). The sequence is characterized by the fact that every number after the first two is the sum of the two preceding ones. By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two. Here the default is 0,1. So in case of input range 10 it will generate 12 numbers. */


#include<stdio.h>

void printFibonacci(int n){


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


    if(n>0){

        sum = first + second;

        first = second;

        second = sum;

        printf("%d ",sum);

        printFibonacci(n-1);

    }

}

void printFibonacci(int);


int main(){

    int k,n;

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

    printf("Enter the nth term: ");

    scanf("%d",&n);


printf("%d %d ",0,1);

printFibonacci(n);

    return 0;

}

Output:
C program which will generate Fibonacci sequence using recursive function


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...