-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlinearSearch.c
More file actions
37 lines (34 loc) · 802 Bytes
/
linearSearch.c
File metadata and controls
37 lines (34 loc) · 802 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
35
36
37
/*Implement Linear search on a List using Array in C.*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i,*a,n,x,flag=0;
printf("Enter the number of elements in the list: ");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
printf("\n");
printf("Enter the elements of the list: ");
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&a[i]);printf("\n");
}
printf("Enter the element to be searched: ");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
printf("Element %d is found at position %d\n",x,i+1);
flag=1;
break;
}
}
if(flag==0)
{
printf("Element %d is not found in the list\n",x);
}
}