C++ or C program to print Fibonacci Series of N numbers using Recursion/ Recursive Function.

File name:- fibrec.cpp
#include<iostream.h>
#include<conio.h>
int main(){
int n,c,i;
int fib(int x);
clrscr();
cout<<"How many terms do you want in Fibonacci series"<<endl;
cin>>n;
if(n>0){
cout<<"Fibonacci series is: ";
for(i=0;i<=n-1;i++){
c=fib(i);
cout<<c<<" ";
}
}
else{
cout<<"Wrong term inserted"<<endl;
}
getch();
return 1;
}
int fib(int x){
if(x==0)return 0;
if(x==1)return 1;
return (fib(x-2)+fib(x-1));
}

File name:- fibrec.c
#include<stdio.h>
#include<conio.h>
int main(){
int n,c,i;
int fib(int x);
clrscr();
printf("How many terms do you want in Fibonacci series\n");
scanf("%d",&n);
if(n>0){
printf("Fibonacci series is:\n");
for(i=0;i<=n-1;i++){
c=fib(i);
printf("%d ",c);
}
}
else{
printf("Wrong term inserted\n");
}
getch();
return 1;
}
int fib(int x){
if(x==0)return 0;
if(x==1)return 1;
return (fib(x-2)+fib(x-1));
}

No comments:

Post a Comment

Priority Queue

Priority queue:-  It is a special type of queue which stores group of elements. Each element has a priority number associated with it. Prior...