Write a C program for Fibonacci series using Function and recursion.

TASK - 

Write a c program which produce Fibonacci series using function and recursion.

Fibonacci series:

Fibonacci sequence of series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, The next number is obtained by combining two numbers before it.



CODE:

#include<stdio.h>
void series(int x);

int main(){
                int n;
printf("Enter the number of terms you want: ");
scanf("%d",&n);
printf("%d\n%d",0,1);
series(n-2); // n-2 because 1st two term are already printed

getchar();
return 0;
}
//Here function begins

void series(int x)
{
                static int n1=0,n2=1,n3;
               

                if (x>0)
                {
                n3=n1+n2;
                n1=n2;
                n2=n3;
                

                printf("\n%d",n3);
                series(x-1); //recalling of function make loop

}
}

Expected Output:








NEED WORK PRODUCE WORK

My name is Abdul Rehman I am from Pakistan. I am doing BS in Computer and information sciences. Currently, I am creating these blogs to help students of computer sciences all over the world..

Post a Comment (0)
Previous Post Next Post