C Saved Program: quiescent-dog

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


#include<stdio.h>

 #include<time.h>
int main() {
   
 
 //void main(){
 clock_t t;
 t=clock();
 int array[15],first,last,middle,n,search,c;

 printf("enter the number of elements in an array \n" );
 
scanf("%d",&n);
 
printf("enter %d integers \n",n);
 
for(c=0;c<n;c++)

 {
 scanf("%d",&array[c]);
 }

 printf("enter the number to be searched \n");
 scanf("%d",&search);

first=0;
last=n-1;

middle=(first+last)/2;

while(first<=last)
 
 {

  if (array[middle]<search)
first=middle+1;
else if(array[middle]==search)
{
  printf("%d is present at location %d \n",search,middle+1);
  break;}
if(first>last)
   printf(" it is not present at location \n");
  t=clock()-t;
  double time_taken=((double)t)/CLOCKS_PER_SEC;
  printf("it took %f seconds to execute \n",time_taken);
  //getch();
  return 0;

}

Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs