Skip to content

Instantly share code, notes, and snippets.

@snadahalli
Last active August 6, 2017 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snadahalli/0b170ef7c9f4a39dc7381d8e33f7b9bc to your computer and use it in GitHub Desktop.
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.
#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