C Program to Implement Linear Search

Introduction

Linear search is a simple searching algorithm that checks each element in an array sequentially until the desired element is found or the end of the array is reached. This guide will show you how to write a C program to implement linear search.

Problem Statement

Create a C program that:

  • Takes the size of the array as input from the user.
  • Takes the elements of the array as input.
  • Takes the key element to search for in the array.
  • Performs a linear search to find the key element.
  • Displays the index of the key element if found, or a message indicating that the element is not found.

Example:

  • Input: Array size = 5, Elements = [10, 23, 45, 9, 15], Key = 45
  • Output: Element found at index 2

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the array size, the array elements, the key element, and the index.
  4. Input the Array Size: Use scanf to take input from the user for the size of the array.
  5. Input the Array Elements: Use a loop to take input from the user for the elements of the array.
  6. Input the Key Element: Use scanf to take input from the user for the key element to search for.
  7. Perform Linear Search: Use a loop to iterate through the array and compare each element with the key.
  8. Display the Result: If the key is found, display its index; otherwise, display a message indicating that the element is not found.

C Program

#include <stdio.h>

/**
 * C Program to Implement Linear Search
 * Author: https://www.javaguides.net/
 */
int main() {
    // Step 1: Declare variables to hold the array size, elements, key, and index
    int n, key, i;

    // Step 2: Prompt the user to enter the size of the array
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);

    // Step 3: Declare an array to hold the elements
    int arr[n];

    // Step 4: Input the array elements
    printf("Enter %d elements:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Step 5: Prompt the user to enter the key element to search for
    printf("Enter the element to search: ");
    scanf("%d", &key);

    // Step 6: Perform linear search
    for (i = 0; i < n; i++) {
        if (arr[i] == key) {
            // If the key is found, display its index and exit
            printf("Element found at index %d\n", i);
            return 0;
        }
    }

    // Step 7: If the loop completes, the element was not found
    printf("Element not found in the array\n");

    return 0;  // Step 8: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variable n is declared to store the size of the array. key stores the element to be searched, and i is used as a loop counter.

Step 2: Input the Array Size

  • The program prompts the user to enter the size of the array using printf. The scanf function then reads the input and stores it in the variable n.

Step 3: Declare the Array

  • The program declares an array arr of size n to hold the elements provided by the user.

Step 4: Input the Array Elements

  • The program uses a for loop to take input for each element of the array. The loop iterates from 0 to n-1, reading the elements using scanf.

Step 5: Input the Key Element

  • The program prompts the user to enter the key element they wish to search for in the array.

Step 6: Perform Linear Search

  • The program uses a for loop to iterate through the array and compare each element with the key:
    • If the key is found, the program displays the index of the key element using printf and exits the program.
    • If the loop completes without finding the key, the program concludes that the element is not in the array.

Step 7: Display the Result

  • If the key element is found, its index is displayed. If not, a message is displayed indicating that the element was not found in the array.

Step 8: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example:

Enter the number of elements in the array: 5
Enter 5 elements:
10
23
45
9
15
Enter the element to search: 45
Element found at index 2

Example (Element Not Found):

Enter the number of elements in the array: 4
Enter 4 elements:
7
22
31
40
Enter the element to search: 25
Element not found in the array

Conclusion

This C program demonstrates how to implement a linear search algorithm. It covers basic concepts such as arrays, loops, and conditional statements, making it a useful example for beginners learning C programming and basic searching techniques.

Leave a Comment

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

Scroll to Top