Linear Search

/*linear search*/

#include <stdio.h>
#include <conio.h>
void main()
{
int a[50],i,n,num,t;
clrscr ();
printf ("Enter number of elements in list :");
scanf ("%d",&n);
for (i=0;i<n;i++)
{
printf ("Enter %d element :",i+1);
scanf ("%d",&a[i]);
}
do
{
t=0;
printf ("Enter number to be searched :");
scanf ("%d",&num);
for (i=0;i<n;i++)
{
 if (a[i]==num)
 {
  t=1;
  break;
 }
}
if (t==1)
printf ("Element %d found at position %d in the list.",num,i+1);
else
printf ("Element %d is not in list.",num);
printf ("\nDo u want to:\n1.Continue\n2.Exit\n");
scanf ("%d",&i);
}while (i==1);
}

Output

OUTPUT:

Enter number of elements in list :5
Enter 1 element :23
Enter 2 element :56
Enter 3 element :33
Enter 4 element :88
Enter 5 element :5
Enter number to be searched :11
Element 11 is not in list.
Do u want to:
1.Continue
2.Exit
1
Enter number to be searched :88
Element 88 found at position 4 in the list.
Do u want to:
1.Continue
2.Exit
Title: Linear Search C program (272 clicks)
Caption: C program for linear searching
Filename: linearsearch.zip
Size: 2 KB