C Program to perform Linear Search, Linear Search basically iterates from start to end in order to find the key element.
#include<stdio.h> int main(){ int search_array[] = {4,1,56,7,10,71,63,17,2,3}; int key = 17; //Element to search int i, size = sizeof(search_array)/sizeof(search_array[0]); int found = 0, position; for(i = 0 ; i < size; i++){ if(search_array[i] == key){ found = 1; position = i; break; } } if(found){ printf("Element %d found at position %d",key,position); }else{ printf("Element not found"); } return 0; }
Element 17 found at position 7
Comments :