C Program Linear Search in Array

By | March 8, 2020

C Program Linear Search in Array

#include <stdio.h>
int main()
{
  int a[100], item, i, n;

  printf("Enter number of elements in array[Maximum 100]=");
  scanf("%d", &n);

  
  for (i = 0; i < n; i++)
    {
    printf("Enter Element Number %d in Array=", i+1);
	scanf("%d", &a[i]);
	}

  printf("Enter a number to search in array=");
  scanf("%d", &item);

  for (i = 0; i < n; i++)
  {
    if (a[i] == item)    /* If required element is found */
    {
      printf("%d is present at location %d in the given array", item, i+1);
      break;
    }
  }
  if (i == n)
    printf("%d isn't present in the given array.\n", item);

  return 0;
}

Output

Enter number of elements in array[Maximum 100]=5
Enter Element Number 1 in Array=66
Enter Element Number 2 in Array=123
Enter Element Number 3 in Array=456
Enter Element Number 4 in Array=55
Enter Element Number 5 in Array=9
Enter a number to search in array=456
456 is present at location 3 in the given array

Output 2

Enter number of elements in array[Maximum 100]=10
Enter Element Number 1 in Array=12
Enter Element Number 2 in Array=34
Enter Element Number 3 in Array=56
Enter Element Number 4 in Array=78
Enter Element Number 5 in Array=90
Enter Element Number 6 in Array=9
Enter Element Number 7 in Array=8
Enter Element Number 8 in Array=5
Enter Element Number 9 in Array=4
Enter Element Number 10 in Array=2345
Enter a number to search in array=23
23 isn’t present in the given array.

 

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *