Linear Search in C Program Using Array

Linear search in c program using array. In this program, we can execute a linear search algorithm in c using an array for beginners.

Linear Search Definition in C

The simplest way of searching a data set is a linear search. Each piece of data is reviewed one by one, beginning at the beginning of the data collection, until a match is found. The search is over once the item is located.

The sequential search, often known as a linear search, is one of the most basic and straightforward searches. When a list has only a few elements and a single search is conducted in an unordered list, linear searching is utilized.

Array Definition in C

An array is a collection of similar elements that are stored in contiguous memory locations and maybe retrieved individually using the index of a unique identifier. 

A collection of data components of the same type stored in contiguous memory regions is referred to as an array. In the C programming language, arrays are a derived data type that can hold primitive data types such as int, char, double, float, and so on.

Linear Search in C Program Using Array

#include<stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter Numbers\n");
scanf("%d", &n);
printf("Enter %d Integer\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a Number to Search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) 
{
printf("%d Is Present At Location %d\n", search, c+1);
break;
}
}
if (c == n)
printf("%d Number Is Not Available\n", search);
return 0;
}

Output:

Enter Numbers

Enter 4 Integer
45
67
34
67
Enter a Number to Search
45
45 Is Present At Location 1