Source code:
/*A Linear search is a searching algorithm which is used to find whether a given number is present in an array. It is also known as sequential search. It is straightforward and keep on comparing each element with the element to search until it is found or the list ends. First an array of size 10 will be taken. We will fill it by reading 10 integers. Then the key integer to be searched will be given as input. The test case total number of values are 11. The first 10 values are to fill up the array. If it is found then output would return it and it's position. */
#include <stdio.h>
int main()
{
int array[10], search, i, n=10;
/* Read array */
printf("Enter the array:");
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
/* Read the interger to be searched */
printf("Enter the search element:");
scanf("%d", &search);
for (i = 0; i < n; i++)
{
if (array[i] == search) /* if required element found */
{
printf("%d is found at position %d.", search, i+1);
break;
}
}
if (i == n)
printf("%d is not there in the array.", search);
return 0;
}
Output:
No comments:
Post a Comment