Skip to content

Commit 80e34d7

Browse files
solved the linear search problem using c
1 parent c62a3ba commit 80e34d7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
3+
int linearSearch(int arr[], int size, int key) {
4+
for (int i = 0; i < size; i++) {
5+
if (arr[i] == key) {
6+
return i; // Return the index if the key is found
7+
}
8+
}
9+
return -1; // Return -1 if the key is not found
10+
}
11+
12+
int main() {
13+
int arr[] = {10, 20, 30, 40, 50};
14+
int size = sizeof(arr) / sizeof(arr[0]);
15+
int key;
16+
17+
printf("Enter the number to search: ");
18+
scanf("%d", &key);
19+
20+
int result = linearSearch(arr, size, key);
21+
22+
if (result != -1) {
23+
printf("Element found at index %d\n", result);
24+
} else {
25+
printf("Element not found in the array\n");
26+
}
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)