Thu Jun 14 2018

Linear Search

C Programming924 views

File Name: linear-search.c

#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!
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.