0% found this document useful (0 votes)
15 views2 pages

Chep2 Ka C Code

hlw im ubaid and im studying comp eng;

Uploaded by

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

Chep2 Ka C Code

hlw im ubaid and im studying comp eng;

Uploaded by

jackmarquina080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>

// Function to perform linear search

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

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

if (arr[i] == target) {

return i; // Return the index where target is found

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

int main() {

int arr[100], size, target, result;

// Input the number of elements in the array

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

scanf("%d", &size);

// Input the elements of the array

printf("Enter %d elements:\n", size);

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

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

// Input the element to be searched

printf("Enter the element to search: ");

scanf("%d", &target);
// Perform linear search

result = linearSearch(arr, size, target);

// Output the result

if (result != -1) {

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

} else {

printf("Element %d not found in the array.\n", target);

return 0;

You might also like