Skip to content

Instantly share code, notes, and snippets.

@complxalgorithm
Created May 8, 2016 08:31
Show Gist options
  • Save complxalgorithm/7045004c7c873e08b19e1fb2085b1c62 to your computer and use it in GitHub Desktop.
Save complxalgorithm/7045004c7c873e08b19e1fb2085b1c62 to your computer and use it in GitHub Desktop.
C program that implements linear search by inputting a number of values for user to input, and then the user will input a number to see if it was included in the original input (list of numbers).
/*
* C program to input N numbers and store them in an array.
* Do a linear search for a given key and report success
* or failure.
*/
#include <stdio.h>
void main()
{
int array[10];
int i, num, keynum, found = 0;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%dn", array[i]);
}
printf("Enter the element to be searched \n");
scanf("%d", &keynum);
/* Linear search begins */
for (i = 0; i < num ; i++)
{
if (keynum == array[i] )
{
found = 1;
break;
}
}
if (found == 1)
printf("Element is present in the array\n");
else
printf("Element is not present in the array\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment