Skip to content

Commit

Permalink
Merge pull request Burnout-Devil#32 from LostHeart-Gaming/patch-2
Browse files Browse the repository at this point in the history
linear search in c
  • Loading branch information
Burnout-Devil committed Oct 18, 2022
2 parents dc93e28 + 0dcf56f commit 3729356
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions linearSeach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#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()
{
int a[]={1,2,3,4,5,6};
int val=4;
int n=sizeof(a)/sizeof(a[0]);
int res = linearSearch(a, n, val); // Store result
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of array", res);
return 0;
}

0 comments on commit 3729356

Please sign in to comment.