Tuesday, 17 December 2019

Linear Search in C

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int main()
{
int i,num,f=0,pos=-1,*arr,n;
printf("\n Enter the number of elements ");
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
  printf("\n Enter element %d :",i);
  scanf("%d",(arr+i));
}
printf("\n Enter the element to be searched ");
scanf("%d",&num);
for(i=0;i<n;i++)
{
  if(*(arr+i)==num)
  {
  f=1;
  pos=i;
  break;
  }
}
if(f==1)
printf("\n %d is found at the position %d. ",num,pos);
if(f==0)
printf("\n The searched number %d does not exist in the array. ",num);
getch();
return 0;
}

No comments:

Post a Comment

Convey your thoughts to authors.