forked from suman-shah/c-program-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear_Search.c
39 lines (33 loc) · 935 Bytes
/
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
// Linear search is a search that finds an element in the list by searching the element sequentially until the element is found in the list.
//This program is for an element occuring once in a list.
#include <stdio.h>
void main()
{
int m, i, n, count = 0;
//Entering number of elements in the array
printf("Enter the no. of elements: ");
scanf("%d", &m);
int a[m];
//Entering the elements
printf("Enter the elements: ");
for (i = 0; i < m; i++)
{
scanf("%d", &a[i]);
}
//Linear search begins
printf("Enter the number you want to search for: ");
scanf("%d", &n);
for (i = 0; i < m; i++)
{
if (a[i] == n)
{
printf("Your number %d is at location %d", n, i);
count++;
break;
}
}
if (count == 0)
{
printf("Your Number %d is not found", n);
}
}