1.fibonacci Series Program in C Using Recursion
1.fibonacci Series Program in C Using Recursion
#include <stdio.h>
int fibonacci(int);
int main()
{
int n,i=0,c;
printf("Enter the n value:");
scanf("%d",&n);
printf("fibonacci series\n");
for(c=1;c<=n;c++)
{
printf("%d\n",fibonacci(i));
i++;
}return 0;
}
int fibonacci(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2));
}
output:
enter the n value:7
fibonacci series:
0
1
1
2
3
5
8
FIBONACCI SERIES PROGRAM IN C FOR NON- RECURSIVE
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z,n,i;
clrscr();
x=0;
y=1;
printf("\n enter value of n:");
scanf("%d",&n);
Search\n
int mid;
delay(1000);
mid=(low+high)/2;
if(low>high)
return -1;
if(k==a[mid])
return(mid);
else
if(k<a[mid])
return binsearch(n,a,k,low,mid-1);
else
return binsearch(n,a,k,mid+1,high);
}
int linsearch(int n,int a[],int k)
{
delay(1000);
if(n<0)
return -1;
if(k==a[n-1])
return(n-1);
else
return linsearch(n-1,a,k);
}
OUTPUT
Case 1
.....MENU.....
1.Binary Search
2.Linear Search
3.Exit
Enter your choice
1
Enter the number of elements
3
Enter the elements of an array
4
8
12
Enter the elements to be searched
12
Element 12 is found at position 2
Case 2
.....MENU.....
1.Binary Search
2.Linear Search
3.Exit
Enter your choice
2
Enter the number of elements
4
Enter the elements of an array
3
6
9
12
Enter the elements to be searched
9
Element 9 is found at position 3
Time taken is 3.021978 CPU cycles