Write a c program to perform linear Search

 #include <stdio.h>


// Function to perform linear search

int linearSearch(int arr[], int n, int target) {

    for (int i = 0; i < n; i++) {

        if (arr[i] == target) {

            return i; // Return the index of the target element

        }

    }

    return -1; // Return -1 if the target element is not found

}


// Function to print the array

void printArray(int arr[], int size) {

    for (int i = 0; i < size; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

}


int main() {

    int n, target;


    // Input the number of elements in the array

    printf("Enter the number of elements: ");

    scanf("%d", &n);


    int arr[n];


    // Input the elements of the array

    printf("Enter the elements: ");

    for (int i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }


    // Input the target value to search for

    printf("Enter the target value: ");

    scanf("%d", &target);


    // Perform linear search

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


    // Print the result

    if (result != -1) {

        printf("Element found at index: %d\n", result);

    } else {

        printf("Element not found in the array.\n");

    }


    return 0;

}


Comments

Popular posts from this blog

Write a c program to Create a Circular Linked list and perform Following Operations A. Insertion At Beginning B. Insertion At End C. Insertion After a particular node Insertion Before a particular node E. Insertion at specific position F. Search a particular node G. Return a particular node H. Deletion at the beginning I. Deletion at the end J. Deletion after a particular node K. Deletion before a particular node L. Delete a particular node M. Deletion at a specific position

Write a c program to check whether the created linked list is palindrome or not

Write a c program to Create a Circular single Linked list and perform Following Operations A. Insertion After a particular node B. Insertion Before a particular node C. Search a particular node D. Return a particular node E. Deletion before a particular node F. Delete a particular node