-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear_Search.c
More file actions
34 lines (34 loc) · 847 Bytes
/
Linear_Search.c
File metadata and controls
34 lines (34 loc) · 847 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
#include <stdio.h>
int linearSearch(int a[], int n, int val)
{
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i + 1;
}
return -1;
}
int main()
{
printf("Enter Number of Elements:");
int n;
scanf("%d",&n);
printf("Enter each element in order\n");
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int value;
printf("Enter Value to be searched:");
scanf("%d",&value);
int store = linearSearch(arr, n, value);
printf("The elements of the array are: ");
for (int i = 0; i < n; i++)
printf("%d\t", arr[i]);
printf("\nElement to be searched is:%d", value);
if (store == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of array", store);
return 0;
}