-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlinear search.c
33 lines (23 loc) · 1.07 KB
/
linear search.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
/* Program to perform linear search on integers */
#include <stdio.h>
int main(void) {
// start of main
int a[100], i, n, key; // declaring the required variable
printf("enter numbers of elemnts:"); // taking input from the user
scanf("%d", &n); // storing the data in a variable
printf("\nenter %d elements\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]); // taking user entered numbers and stroing it in a array
}
printf("enter the element to be searched:\n"); // asking the user to enter the element to be searched
scanf("%d", &key); // storing the user entered data in a variable
for (i = 0; i < n; i++) {
if (a[i] == key) { // checking the wether the key is equal to element in that array and if true display the message
printf("search sucessful!\n",
key, a[i]);
exit(0);
}
} // end of for loop
printf("search unsucessful!\n element %d is not found in the given list!\n", key); // if not found in the list display key not found
return 0; // end of main
}