-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_search.c
52 lines (41 loc) · 1.17 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Ram is given n boxes kept one after the other each containing a number inside it. The boxes are indexed from 0 to n-1.
// Now Ram is asked to find the position of the box in which a specific number is stored.
// Ram opens each box one-by-one and checks whether that box contains the given number or not.
// Implement the above scenario using linear search technique.
// Input Format
// The First line reads an integer representing the number of boxes.
// The Second line reads the Numbers (elements) hidden in the boxes.
// The Third line reads the Number to be searched .
// Output format
// Display the position(index) of the box in which the Number is found.
// if not found,display"Number not found."
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int x, i, flag = 0;
scanf("%d", &x);
for (i = 0; i < n; i++)
{
if (arr[i] == x)
{
flag = 1;
break;
}
}
if (flag == 1)
{
printf("Number found at location : %d", i);
}
else
{
printf("Number not found.");
}
return 0;
}