Code Scrappers


Linear Search - C program


                    #include<stdio.h>
                    #include<stdlib.h>

                    int main()
                    {
                        int a[50], key, n, i, j;

                        printf("\nEnter the number of elements\n");

                        scanf("%d",&n);

                        printf("\nEnter %d elements\n", n);

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

                        }

                        printf("\nEnter the element to be searched\n");

                        scanf("%d",&key);

                        for(i=0;i<n;i++)
                        {
                            if(key==a[i])
                            {
                                printf("\nElement %d found at %d position\n",key, i+1);

                                exit(0);

                            }

                        }

                        printf("\nElement not found\n");

                    }



                



Output

          Enter the number of elements
          5

          Enter 5 elements
          4
          7
          2
          8
          6

          Enter the element to be searched
          8

          Element 8 found at 4 position