Pages

Sunday, 17 February 2019

Program to illustrate Linear Search

// Program to illustrate Linear Search
    #include <stdio.h>
    int main()
    {
      int array[100], search, i, n;
      printf("Enter number of elements in array\n");
      scanf("%d", &n);
      printf("Enter %d integer(s)\n", n);
      for (i = 0; i < n; i++)
        {
         scanf("%d", &array[i]);
        }
      printf("\n Array elements are :");
      for (i = 0; i < n; i++)
        {
        printf("\n Address :%p --> Array : array[%d] Data : %d Location : %d", &array[i],i,array[i],i+1);
        }
      printf("\nEnter a number to search :");
      scanf("%d", &search);
      for (i = 0; i < n; i++)
      {
        if (array[i] == search)    /* If required element is found */
        {
          printf("%d is present at location %d.\n", search, i+1);
          break;
        }
      }
      if (i == n)
        printf("%d isn't present in the array.\n", search);
      return 0;
    }

output:

No comments:

Post a Comment

Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...