Friday, 22 June 2012

write a c program to search given element into the list using linear search method


/*write a c program to search given element into the list using
linear search method*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,num,i,flag=0;
clrscr();
printf("\nenter how many number you want:-");
scanf("%d",&n);
printf("\nenter the element:-");
for(i=0;i<n;i++)
scanf("\n%d",&a[i]);
printf("\nenter the which number you want to search:-");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
flag=1;
break;
}
}
if(flag==1)
printf("\nthe number %d is found at %d location",num,i+1);
else
printf("\nnumber not found");
getch();
}
/*---------------OUTPUT--------------------
enter how many number you want:-4

enter the element:-7
8
9
1

enter the which number you want to search:-9

the number 9 is found at 3 location



enter how many number you want:-4                                              
                                                                               
enter the element:-7                                                          
8                                                                              
5                                                                              
6                                                                              
                                                                               
enter the which number you want to search:-1                                  
                                                                               
number not found              */

No comments: