Posts

Showing posts with the label Write a C program to find Binary Search Program in C program( Data Structure And Algorithm)

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; } } } ___________________________ _____________________________________...