From 8dc8f4d4c34b4020965820aefb9f7a89c16d6c8b Mon Sep 17 00:00:00 2001 From: premakhil Date: Sat, 3 Oct 2020 14:21:24 +0530 Subject: [PATCH] Added Linear Search to C folder --- C/Linear_search.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 C/Linear_search.c diff --git a/C/Linear_search.c b/C/Linear_search.c new file mode 100644 index 0000000..60e6a65 --- /dev/null +++ b/C/Linear_search.c @@ -0,0 +1,39 @@ +//Linear Search alogorithm without recursion, in C language. + +#include +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"); + } +} \ No newline at end of file