-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path12_search_element.c
55 lines (45 loc) · 1.3 KB
/
12_search_element.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
51
52
53
54
55
// // Write a program in C to search an element in given array using linear search.
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
// // Main Function Start
int main()
{
const int ARRAY_SIZE;
int found = 0, search;
printf("\nHow Many Elements You Want to Enter => ");
scanf("%d", &ARRAY_SIZE);
if (ARRAY_SIZE < 1)
{
printf("\n!!! Invalid Input, Plz Correctly Specify Number of Elements...");
exit(0);
}
// // Declare Array of Variable size
int arr[ARRAY_SIZE];
// // Input Elements
printf("\nEnter %d Elements => ", ARRAY_SIZE);
for (int i = 0; i < ARRAY_SIZE; i++)
scanf("%d", &arr[i]);
// // Print Elements
puts("\n>>>>>>>> Elements of Array <<<<<<<<<");
for (int i = 0; i < ARRAY_SIZE; i++)
printf("%d ", arr[i]);
// // Get Element to be Searched
printf("\n\nEnter An Element to Search In Array => ");
scanf("%d", &search);
// // Search Element Using Linear Search
for (int i = 0; i < ARRAY_SIZE; i++)
{
if (arr[i] == search)
found = 1;
}
if (found)
printf("\nYes, %d is Present in Array", search);
else
printf("\nNo, %d is Not Present in Array", search);
putch('\n');
getch();
return 0;
}
// // Main Function End