Linear Search

C Programming

C Programming Examples

#include<stdio.h>

int main() {
	int data[ ] = {5,9,8,1,6,4,2,7,3,0}, i, search, flag = 0;
	printf("Enter a number to search:\n");
	scanf("%d", &search);
	for(i = 0; i < 10; i++) {
		if(data[i] == search) {

			/* If search value matched */
			printf("%d is found at location %d!\n",search, i+1);
			flag = 1;

			/* Terminate 'for' loop */
			break;
		}
	}
	if(flag == 0)
		printf("%d is not found!\n",search);
	return 0;
}


          /****** Output ******/
          

Enter a number to search:


4


4 is found at location 6!




----------------------------------




Enter a number to search:


10


10 is not found!

Learn more about Linear Search