## Searching algorithm: linear search in C

Linear search is a searching algorithm which is also known as sequential search. By  this linear searching algorithm we will search whether an element is exist or not in the array. If we find then what is the location of that element.

Linear search compares the expected element with the element of array one by one till the last element if not fount. In this article we will see some program related to linear search in C.

## C program using linear search

``````// c program implementing linear search algorithm

#include <stdio.h>

int main(){
int a, Num, search, myarr[50];

printf("Enter number of elements of the array here : ");
scanf("%d", &Num);

printf("Enter %d integers as array element : \n", Num);

for(a = 0; a < Num; a++){
scanf("%d", &myarr[a]);
}

printf("Enter which number you want to search : ");
scanf("%d", &search);

for(a = 0; a < Num; a++){
if (myarr[a] == search){
printf("\nNumber (%d) is present at location %d.\n", search, a+1);
break;
}
}
if(a == Num){
printf("\n%d is not present here in the array.\n", search);
}
return 0;
}``````

## Linear search for multiple occurrence in C

``````// linear search algorithm in c for multiple matching

#include <stdio.h>

int main(){
int Num, myarr[50], sr, counter = 0, k;

printf("Enter number of integers in the array : ");
scanf("%d", &Num);

printf("\nEnter %d integers bellow : \n", Num);

for (k = 0; k < Num; k++){
scanf("%d", &myarr[k]);
}
printf("\nEnter which integer you want to search : ");
scanf("%d", &sr);

for (k = 0; k < Num; k++){
if (myarr[k] == sr){
printf("\n%d is found at location %d.\n", sr, k + 1);
counter++;
}
}
if (counter == 0){
printf("\n%d is not present here in given array.\n", sr);
}else{
printf("\n%d is found %d times in your given array.\n", sr, counter);
}
return 0;
}``````

### Program output will be as follows

``````Enter number of integers in the array : 10

Enter 10 integers bellow :
12 23 34 45 23 45 23 65 23 15

Enter which integer you want to search : 23

23 is found at location 2.

23 is found at location 5.

23 is found at location 7.

23 is found at location 9.

23 is found 4 times in your given array.``````
``````Enter number of integers in the array : 5

Enter 5 integers bellow :
100 200 400 300 50

Enter which integer you want to search : 700

700 is not present here in given array.``````

You can also make the program using function as well as recursion. Now, let’s try your own to implement linear search algorithm in C. Best of luck!

Previous pageBinary search

Next pageLargest element in array

Recommended for you: