forked from Chanchal2125/It-is-For-Everyone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearSearch.c
50 lines (40 loc) · 1 KB
/
LinearSearch.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
44
45
46
47
48
49
50
#include <stdio.h>
int findPosition(int arr[], int size, int key)
{
int pos;
for (pos = 0; pos < size; pos++)
{
if (arr[pos] == key)
return pos + 1;
}
return -1;
}
int main()
{
int size, found, key, index;
int arr[100];
printf("<------ LINEAR SEARCH PROGRAM ------> \n\n");
printf("--> Enter size of array : ");
scanf("%d", &size);
for (int el = 1; el <= size; el++)
{
printf(" \n==> Enter element %d : ", el);
scanf("%d", &arr[el - 1]);
}
printf("------------------------------------- \n");
printf("==> ARRAY: ");
printf("[");
for (int i = 0; i < size - 1; i++)
{
printf("%d, ", arr[i]);
}
printf("%d]\n\n", arr[size - 1]);
printf("--> Enter the element to search : ");
scanf("%d", &key);
index = findPosition(arr, size, key);
if (index != -1)
printf("FOUND! %d is present at %d.", key, index);
else
printf("OOPS! %d isn't present.", key);
return 0;
}