Last active
August 6, 2017 15:33
-
-
Save snadahalli/0b170ef7c9f4a39dc7381d8e33f7b9bc to your computer and use it in GitHub Desktop.
C Program to demonstrate the working of linear search. We take an array of integers and search it for a given key.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <conio.h> | |
void main() | |
{ | |
int array[10]; | |
int i, N, keynum, found=0; | |
printf("Enter the value of N\n"); | |
scanf("%d",&N); | |
printf("Enter the elements one by one\n"); | |
for(i=0; i<N ; i++) | |
{ | |
scanf("%d",&array[i]); | |
} | |
printf("Input array is\n"); | |
for(i=0; i<N ; i++) | |
{ | |
printf("%d\n",array[i]); | |
} | |
printf("Enter the element to be searched\n"); | |
scanf("%d", &keynum); | |
for ( i=0; i < N ; i++) | |
{ | |
if( keynum == array[i] ) | |
{ | |
found = 1; | |
break; | |
} | |
} | |
if ( found == 1) | |
printf("Element found in the given array"); | |
else | |
printf("Element was not found in the array"); | |
} /* End of main */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment