Program to search for a number in a list using linear search.

 #include<stdio.h>

main()

{

int a[100],i,n,ele,pos,flag=0;

clrscr();

printf("\nEnter the total number of elements\n");

scanf("%d",&n);

printf("\nEnter the elements one by one: \n");

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

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

printf("\nEnter the element to be searched\n");

scanf("%d",&ele);

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

{

if(a[i]==ele)

{

flag=1;

break;

}

}

if(flag==1)

printf("\n%d found in %d position",ele,i+1);

else

printf("\n%d NOT FOUND!!!\n",ele);


getch();

}



Output:

  1. Enter the total number of elements

5

Enter the elements one by one:

12   67   54   89   34

Enter the element to be searched

89

89 found at position 4


  1. Enter the total number of elements

4

Enter the elements one by one:

40  23  56  55

Enter the element to be searched

30

30 NOT FOUND!!!




Post a Comment

Previous Post Next Post