-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelementarray.c
More file actions
36 lines (28 loc) · 739 Bytes
/
Copy pathelementarray.c
File metadata and controls
36 lines (28 loc) · 739 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
35
36
#include <stdio.h>
int main() {
int n, i, key, found = 0;
// Input the number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
// Input the elements
printf("Enter the elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}
// Input the element to be searched
printf("Enter the element to search: ");
scanf("%d", &key);
// Linear search
for(i = 0; i < n; ++i) {
if(arr[i] == key) {
printf("%d found at position %d\n", key, i+1);
found = 1;
break;
}
}
if(!found) {
printf("%d not found in the array.\n", key);
}
return 0;
}