Code Scrappers


Linear Search C program


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

                    void main()
                    {
                        int a[10], n, key, found=0, i, pos;

                        printf("enter the array size\n");

                        scanf("%d",&n);

                        printf("enter the array elements\n");

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

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

                        scanf("%d",&key);

                        for(i=0;i<n;i++)
                        {
                            if(a[i] == key)
                            {

                                found++;

                                pos = i+1;
                            }
                        }

                        if(found == 0)
                        {
                            printf("element not found");
                        }

                        else
                        {
                            printf("Element %d is found at %dth position",key, pos);
                        }
                    }

              



Output

        enter the array size
        5

        enter the array elements
        1
        2
        3
        4
        5

        enter the element to be searched
        2

        Element 2 is found at 2th position