Header Ads Widget

C PROGRAM: Program to implement linear search in C language - Sparsh Blog

 Ans:->



The program to implement linear search in C languae.

Program is as follows:--


#include <stdio.h>

int main()

{

        int array[100], search, c, n;

        printf("Enter number of elements in array:");

        scanf("%d", &n);

        printf("Enter %d integer(s)\n", n);

        for (c = 0; c < n; c++)

        scanf("%d", &array[c]);    

        printf("Enter a number to search:");

        scanf("%d", &search);

        for (c = 0; c < n; c++)

        {

                if (array[c] == search) /* If required element is found */

                {

                        printf("%d is present at location %d.\n", search, c+1);

                        break;

                }

        }

        if (c == n)

        printf("%d isn't present in the array.\n", search);

        return 0;

   }


OUTPUT:--

Enter a number of elements in array: 5

Enter 5 integers(5)

45

89

63

14

17

Enter a number to search: 25

25 isn't present in the array.


THANKU FOR READING THIS ARTICLE ON-


If you want to suggest any changes in the program please feel free to contact us in the comment section or you can reach us using Contact us Page.


Post a Comment

0 Comments