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

Linear Search Algorithm in C

This document describes a linear search algorithm to search for an element in an array. It first takes user input for the size of the array and elements of the array. It then takes input for the item to search. It uses a for loop to iterate through each element of the array and compares it to the item. If a match is found, it prints the location. If no match is found after searching the whole array, it prints that the item does not exist.

Uploaded by

KarishmaSingh
Copyright
© Attribution Non-Commercial (BY-NC)
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)
61 views1 page

Linear Search Algorithm in C

This document describes a linear search algorithm to search for an element in an array. It first takes user input for the size of the array and elements of the array. It then takes input for the item to search. It uses a for loop to iterate through each element of the array and compares it to the item. If a match is found, it prints the location. If no match is found after searching the whole array, it prints that the item does not exist.

Uploaded by

KarishmaSingh
Copyright
© Attribution Non-Commercial (BY-NC)
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

www.eazynotes.

com

Gursharan Singh Tatla

Page No. 1

LINEAR SEARCH
/*** Program to Search an Element in the Array using Linear Search ***/ #include <stdio.h> void linear_search(); int a[50], item, n, i; main() { printf("\n\nEnter size of an array: "); scanf("%d", &n); printf("\n\nEnter elements of an array:\n"); for (i=0; i<n; i++) scanf("%d", &a[i]); printf("\nEnter item to search: "); scanf("%d", &item); linear_search(); getch(); } void linear_search() { for (i=0; i<n; i++) if (item == a[i]) { printf("\n\nItem found at location %d", i+1); return; } if (i == n) printf("\n\nItem doesnot exist."); }

You might also like