Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1
#include <stdio.
h>
// Function to perform Linear Search
int linearSearch(int A[], int n, int target) { for (int i = 0; i < n; i++) { if (A[i] == target) { return i; // Return the index if the target is found } } return -1; // Return -1 if the target is not found }
int main() { int n, target;
// Take the size of the array as input
printf("Enter the number of elements: "); scanf("%d", &n);
int A[n];
// Take array elements as input
printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &A[i]); }
// Take the target element to search for as input
printf("Enter the element to search for: "); scanf("%d", &target);
// Call the linearSearch function
int result = linearSearch(A, n, target);
// Print the result
if (result != -1) { printf("Element %d found at position %d\n", target, result); } else { printf("Element %d not found\n", target); }