Sunteți pe pagina 1din 8

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);

printf("\n fibonacci series:\n\n");


printf("\n%d",x);
printf("\t%d",y);
for(i=0;i<n-2;i++)
{
z=x+y;
printf("\t%d",z);
x=y;
y=x;
}
getch();
}
OUTPUT:
Enter the value of n:4
Fibonacci series:
1
1
2
3
Implementation of recursive binary search and linear search
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#define max 20
int pos;
int binsearch(int,int[],int,int,int);
int linsearch(int,int[],int);
void main()
{
int ch=1;
double t;
int n,i,a[max],k,op,low,high,pos;
clock_t begin,end;
clrscr();
while(ch)
{
printf("\n.....MENU.....\n 1.Binary Search\n 2.Linear
3.Exit\n");
printf("\nEnter your choice\n");
scanf("%d",&op);
switch(op)
{

Search\n

case 1:printf("\nEnter the number of elements \n");


scanf("%d",&n);
printf("\nEnter the elements of an array in order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the elements to be searched\n");
scanf("%d",&k);
low=0;high=n-1;
begin=clock();
pos=binsearch(n,a,k,low,high);
end=clock();
if(pos==-1)
printf("\n\n Unsuccessful search");
else
printf("\n Element %d is found at position %d",k,pos+1);
printf("\n Time taken is %lf CPU1 cycles\n",(endbegin)/CLK_TCK);
getch();
break;
case 2:printf("\nEnter the number of elements\n");
scanf("%d",&n);
printf("\nEnter the elements of an array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the elements to be searched\n");
scanf("%d",&k);
begin=clock();
pos=linsearch(n,a,k);
end=clock();
if(pos==-1)
printf("\n\n Unsuccessful search");
else
printf("\n Element %d is found at position
%d",k,pos+1);
printf("\n Time taken is %lf CPU cycles\n",(endbegin)/CLK_TCK);
getch();
break;
default:printf("\nInvalid choice entered\n");
exit(0);
}
printf("\n Do you wish to run again (1/0) \n");
scanf("%d",&ch);
}
getch();
}
int binsearch(int n,int a[],int k,int low,int high)
{

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

Time taken is 1.978022 CPU1 cycles

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

FIND THE GCD USING EUCLIDS ALGORITHM


#include <stdio.h>
#include<conio.h>
int gcd_algorithm(int x, int y)
{
if (y == 0)
{
return x;
}
else if (x >= y && y > 0)
{
return gcd_algorithm(y, (x % y));
}
}
int main(void)
{
int num1, num2, gcd;
clrscr();
printf("\nEnter two numbers to find gcd using Euclidean
algorithm:");
scanf("%d%d", &num1, &num2);
gcd = gcd_algorithm(num1, num2);
if (gcd)
printf("\nThe GCD of %d and %d is %d\n", num1, num2, gcd);
else
printf("\nInvalid input!!!\n");
getch();
}
output:
enter the num to find gcd:
30 72
the gcd of 30 and 72 is 2
IMPLEMENTATION OF SELECTION SORT
#include<stdio.h>
#include<conio.h>
int main()
{
int array[100], n, c, d, position, swap;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d integers\n", n);


for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
for ( c = 0 ; c < ( n - 1 ) ; c++ )
{
position = c;
for ( d = c + 1 ; d < n ; d++ )
{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
getch();
}
output:
enter the number of elemets
3
enter 3 integers
6
2
9
sorted list in ascending order:
2
6
9
IMPLEMENTATION OF BUBBLE SORT
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], n, c, d, swap;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap
= array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
getch();
}
output:
enter the number of elements:
3
enter 3 integers
2
9
6
sorted list in asending order:
2
6
9

S-ar putea să vă placă și