0% found this document useful (0 votes)
4 views1 page

Linear Search

Uploaded by

Yashi Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Linear Search

Uploaded by

Yashi Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
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);
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy