//LINEAR SEARCH
//CODE
#include<stdio.h>
#include<conio.h>
int main()
{
int a[30],key,num,i;
printf("\n Enter number of elements:");
scanf("%d",&num);
printf("\n Enter the numbers:");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the element to be searched:");
scanf("%d",&key);
i=0;
for(i=0;i<num;i++)
{
if(a[i]==key)
{
printf("%d is present in location %d",key,i+1);
break;
}
}
if(i==num)
{
printf("%d is not found in the list",key);
}
getch();
return 0;
}
//OUTPUT
Enter the number of elements to be searched : 5
Enter the numbers : 45
64
12
78
91
Enter the element to be searched : 64
64 is present in location 2
//CODE
#include<stdio.h>
#include<conio.h>
int main()
{
int a[30],key,num,i;
printf("\n Enter number of elements:");
scanf("%d",&num);
printf("\n Enter the numbers:");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the element to be searched:");
scanf("%d",&key);
i=0;
for(i=0;i<num;i++)
{
if(a[i]==key)
{
printf("%d is present in location %d",key,i+1);
break;
}
}
if(i==num)
{
printf("%d is not found in the list",key);
}
getch();
return 0;
}
//OUTPUT
Enter the number of elements to be searched : 5
Enter the numbers : 45
64
12
78
91
Enter the element to be searched : 64
64 is present in location 2
No comments:
Post a Comment
Note: only a member of this blog may post a comment.