0% found this document useful (0 votes)
3 views2 pages

Linear

The document contains a C program that implements a linear search algorithm. It prompts the user to input the number of elements and the elements themselves, then asks for a key to search for in the array. The program outputs the position of the key if found, or indicates that the element is not found.

Uploaded by

Vaishnav D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Linear

The document contains a C program that implements a linear search algorithm. It prompts the user to input the number of elements and the elements themselves, then asks for a key to search for in the array. The program outputs the position of the key if found, or indicates that the element is not found.

Uploaded by

Vaishnav D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#include<stdlib.h>
void main()
{
int a[50],i,n,elt,count,pos=-1,key;
printf("linear search");
printf("\n\t\n\n");
printf("\n\t enter the limit");
scanf("%d",&n);
printf("enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\t the elements are:");
scanf("%d",&key);
pos=linear(key,a,n);
if(pos==-1)
printf("the element not found");
else
printf("the element is in position %d",pos);
return;
}
int linear(int key,int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
if(key==a[i])
return(i+1);
}
return-1;
}

output:
mdit@mdit-desktop:~$ ./a.out
linear search

enter the limit2


enter the elements:1 2

the elements are:3


the element not foundmdit@mdit-desktop:~$ ./a.out
linear search

enter the limit5


enter the elements:1
2
3
4
5

the elements are:2


the element is in position 2mdit@mdit-desktop:~$

You might also like