-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlinear.c
43 lines (43 loc) · 781 Bytes
/
linear.c
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
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdbool.h>
bool linearSearch(int arr[],int l,int r,int key)
{
if(arr[l]==key)
{
return true;
}
if(l==r)
{
return false;
}
bool ans = linearSearch(arr,l+1,r,key);
if(ans)
{
return true;
}
return false;
}
int main()
{
printf("Enter the size of the array\n");
int n;
scanf("%d",&n);
int arr[n];
printf("Enter the elements of the array\n");
for(register int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int key;
printf("Enter the element to find\n");
scanf("%d",&key);
bool result = linearSearch(arr,0,n,key);
if(result)
{
printf("Element found!!!\n");
}
else
{
printf("Element not found\n");
}
}