linear search program in c

Here is a simple implementation of a linear search program in C:

#include <stdio.h>

int linearSearch(int arr[], int n, int key) {
   for (int i = 0; i < n; i++) {
      if (arr[i] == key) {
         return i;
      }
   }
   return -1;
}

int main() {
   int arr[] = {5, 10, 15, 20, 25};
   int n = sizeof(arr) / sizeof(arr[0]);
   int key = 15;

   int result = linearSearch(arr, n, key);

   if (result == -1) {
      printf("Element not found\n");
   } else {
      printf("Element found at index %d\n", result);
   }

   return 0;
}

This program defines a function linearSearch that takes an array arr, its size n, and a key element to search for. It iterates through the array using a loop and checks if each element is equal to the key. If a match is found, the function returns the index of the element. If no match is found, the function returns -1.

In the main function, an example array arr is defined, along with its size n and a key element key to search for. The linearSearch function is then called with these arguments, and the result is stored in the result variable. Finally, the program prints the appropriate message based on the result of the search.

This program assumes that the array elements are of type int. You can modify it to work with other types by changing the data type of the array and the key variable.