forked from casafurix/c-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinSearch.c
More file actions
34 lines (34 loc) · 720 Bytes
/
linSearch.c
File metadata and controls
34 lines (34 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
void readArr(int[],int);
int linSearch(int[],int,int);
void main()
{
int A[20],n,key,i,x;
printf("Enter the number of elements: ");
scanf("%d",&n);
readArr(A,n);
printf("Enter the element to search: ");
scanf("%d",&key);
x=linSearch(A,n,key);
if(x==-1)
printf("Element not found");
else
printf("Element found at position %d",x+1);
}
void readArr(int A[],int n)
{
int i;
printf("Enter %d number of elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
}
int linSearch(int A[],int n,int key)
{
int i;
for(i=0;i<n;i++)
{
if(A[i]==key)
return i;
}
return -1;
}