Skip to content

Commit

Permalink
Added Linear Search to C folder
Browse files Browse the repository at this point in the history
  • Loading branch information
premakhil committed Oct 3, 2020
1 parent 7c8b727 commit 8dc8f4d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions C/Linear_search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//Linear Search alogorithm without recursion, in C language.

#include <stdio.h>
void main()
{
int a[10], n, x, flag = 0, index = 0;
printf("%s", "Enter the number of elements in the array\n");
scanf("%d", &n);
printf("Enter the elements of the array\n");

for (int i = 0; i < n; ++i)
{
scanf("%d", &a[i]);
}

printf("%s", "Enter the element you want to search for\n");
scanf("%d", &x);

for (int j = 0; j < n; ++j)
{
if (a[j] == x)
{
flag = 1;
index = j;
break;
}
}

if (flag == 1)
{
printf("Element found at\t");
printf("%d\n", index);
}

else
{
printf("Element not found\n");
}
}

0 comments on commit 8dc8f4d

Please sign in to comment.