Featured post

-Write C program to find even and odd number using macro

-Write C program to find even and odd number using macro #include<stdio.h> #define number(n)((n%2==0)?1:0) int main() { int num; printf("enter any number"); scanf("%d",&num); if(number(num)) printf("number is even"); else printf("number is odd"); return 0; } ***************************Output*********************** amr@amrj-virtual-machine:~$ gccevenodd.c amr@samr-virtual-machine:~$ ./a.out enter any number 2 number is evensmj@amr-virtual-machine:~$

Write a C program to find Binary Search Program in C program( Data Structure And Algorithm)

                 Data Structure And Algorithm 

Write a C program to find Binary Search Program in C program

#include<stdio.h>

int bserch(int a[10],int key,int lb,int ub);

int main()

{

int i,a[10],n,key,lb,ub,mid;

printf("enter the no=");

scanf("%d",&n);

printf("enter the element=");

for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

}

printf("enter the key=");

scanf("%d",&key);

mid=bserch(a,key,0,n-1);

if(key==a[mid])

{

printf("%d is found at position %d",key,mid+1);

}

else

printf("key is not found");

}

int bserch(int a[10],int key,int lb,int ub)

{

int mid,i,n;

while(lb<=ub)

{

mid=(lb+ub)/2;

if(key<a[mid])

{

ub=mid-1;

bserch(a,key,lb,ub);

}

else

if(key>a[mid])

{

lb=mid+1;

bserch(a,key,lb,ub);

}

else

if(key==a[mid])

{

return mid;

}

}

}_____________________________________________________________________

                          OUTPUT

enter the no=4

enter the element=1

2

3

4

enter the key=3

3 is found at position 3amr@amr-virtual-machine:~$ gcc 1.c -o 1

amr@amr-virtual-machine:~$ ./1

enter the no=4

enter the element=1

2

3

4

enter the key=10

key is not found

_____________________________________________________________________

 


Comments

Popular posts from this blog

-Write C program to find even and odd number using macro

BCA (SCIENCE) JAVA PRACTICAL SLIPS PAGES.(JAVA) UNIVERSITY OF PUNE

How to OCR Text Extraction in Flutter Using Google ML Kit