A program for linear search in C language.
Linear
Search
       In
this article we will see the working of linear search
      In
linear search we accept the elements of array from the user. We ask the user to
enter a number to be  search in the array. If that element or number is found
then we will display the position of the element in that array.
     We
compare the search element sequentially from the first element to the last
element or till the number is found. As we search from first to last in linear
way hence it is known as a linear search.
//This
  program is designed to show you the working of Linear Search 
#include<stdio.h> 
#include<conio.h> 
void
  main() 
{ 
            int i,n,num,a[100],flag=0; 
            clrscr(); 
            printf("\n Enter the number
  of elements "); 
            scanf("%d",&n);
  //take array size from user 
            //accept the array 
            printf("\n Enter the elements
  "); 
            for(i=0;i<n;i++) 
            { 
            scanf("%d",&a[i]); 
            } 
            printf("\n ENter the element
  to be search "); 
            scanf("%d",&num); 
            for(i=0;i<n;i++) 
            { 
                        if(a[i]==num) //check
  with each element 
                        { 
                        flag=1;    // if element is found then stop checking
  ahead 
                        break; 
                        } 
            } 
            if(flag==1) 
            { 
            printf("\n Element is found
  at the position %d ",i+1);//we start from 0 
            }                                    //hence to display correct position
  increment by 1 
            else 
            { 
            printf("\n Element is not
  present in the given array "); 
            } 
            getch(); 
} 
 | 
 
Output: we are
accepting array size as 7 from user. Among entered elements, we need to find
out the position of element 50 in the list. Output shows its position as 5 in
the list. 

#include
ReplyDelete#include
void main()
{
int s[10],search,c,n;
clrscr();
printf("\n enter no element in ");
scanf("%d",&n);
printf("\n\tenter %d element ",n);
for(c=0;c<n;c++)
scanf("%d",&s[c]);
printf("\nenter the search element no");
scanf("%d",&search);
for(c=0;c<n;c++)
{
if(s[c]==search)
{
printf(" %d element is present \n\t preasent element possition %d",search,c+1);
break;
}
}
if(c==n)
printf("element is not present %d",search);
getch();
}
#include
ReplyDelete#include
void main()
{
int array[10],search,i,j;
printf("\n Enter the no of Element in Array:");
scanf("%d", &j);
printf("Enter the %d integer",j);
for(i=0;i<j;i++)
scanf("%d",&array[i]);
printf("Enter the search element:");
scanf("%d",&search);
for(i=0;i<j;i++)
{
if(array[i]==search)
{
printf("%d is present %d",search,i+1);
break;
}
}
#include
ReplyDelete#include
void main()
{
int arr[20];
int i,size,sech;
clrscr();
printf("Enter total no. of elements : ");
scanf("%d",&size);
printf("Enter The Elements:\n");
for(i=0; i<size; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the element to be searched: ");
scanf("%d",&sech);
for(i=0; i<size; i++)
{
if(sech==arr[i])
{
printf("Element exits in the list at position : %d",i+1);
break;
}
}
getch();
}
Linear search
ReplyDelete#include
#include
void main()
{
int s[10],search,c,n;
clrscr();
printf("\n enter no element in ");
scanf("%d",&n);
printf("\n\tenter %d element ",n);
for(c=0;c<n;c++)
scanf("%d",&s[c]);
printf("\nenter the search element no");
scanf("%d",&search);
for(c=0;c<n;c++)
{
if(s[c]==search)
{
printf(" %d element is present \n\t preasent element possition %d",search,c+1);
break;
}
}
if(c==n)
printf("element is not present %d",search);
getch();
}
#include
ReplyDelete#include
void main()
{
int arr[20];
int i,size,sech;
clrscr();
printf("Enter total no. of elements : ");
scanf("%d",&size);
printf("Enter The Elements:\n");
for(i=0; i<size; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the element to be searched: ");
scanf("%d",&sech);
for(i=0; i<size; i++)
{
if(sech==arr[i])
{
printf("Element exits in the list at position : %d",i+1);
break;
}
}
getch();
}
//This program is designed to show you the working of Linear Search
ReplyDelete#include
void main()
{
int i,n,num,a[100],flag=0;
printf("\n Enter the number of elements ");
scanf("%d",&n); //take array size from user
//accept the array
printf("\n Enter the elements ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n ENter the element to be search ");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num) //check with each element
{
flag=1; // if element is found then stop checking ahead
break;
}
}
if(flag==1)
{
printf("\n Element is found at the position %d ",i+1);//we start from 0
} //hence to display correct position increment by 1
else
{
printf("\n Element is not present in the given array ");
}
}
/*Linear search*/
ReplyDelete#include
void main()
{
int i,n,num,a[100],flag=0;
printf("\n Enter the number of elements ");
scanf("%d",&n);
printf("\n Enter the elements ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n ENter the element to be search ");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\n Element is found at the position %d ",i+1);
}
else
{
printf("\n Element is not present in the given array ");
}
}
/**********************************************************
ReplyDeletelinear search
**********************************************************/
#include
int main()
{
int array[100],search,c,n;
printf("\nEnter the number of elements in array:");
scanf("%d",&n);
printf("Enter %d integer(s)\n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("\nEnter the number to search\n:");
scanf("%d",&search);
for(c=0;c<n;c++)
{
if (array[c] ==search)
{
printf("%d is present at location %d\n",search,c+1);
break;
}
}
if(c==n)
printf("%d is not present in array\n",search);
return 0;
}
#include
ReplyDelete#include
void main()
{
int s[10],search,c,n;
clrscr();
printf("\n enter no element in ");
scanf("%d",&n);
printf("\n\tenter %d element ",n);
for(c=0;c<n;c++)
scanf("%d",&s[c]);
printf("\nenter the search element no");
scanf("%d",&search);
for(c=0;c<n;c++)
{
if(s[c]==search)
{
printf(" %d element is present \n\t preasent element possition %d",search,c+1);
break;
}
}
if(c==n)
printf("elem