C Program to Linear Search in an Unsorted Array

This program first takes the size of the array as input from the user, then takes the elements of the array, and finally, takes the element to be searched. The linearSearch function is then called to perform the linear search, and the result is displayed in the main function.

Note that linear search is a simple search algorithm with a time complexity of O(n) for an array of size n. It sequentially checks each element of the array until a match is found or the entire array is traversed.

Program :

#include <stdio.h>
#include <conio.h>
 
void main( )
{
int arr[100];;
int i, num, n;
printf(“Enter the number of Elements : “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Element%d : “,i+1);
scanf(“%d”,&arr[i]);
}
printf ( “Enter number to search: ” ) ;
scanf ( “%d”, &num ) ;
 
for ( i = 1 ; i <= n ; i++ )
{
if ( arr[i] == num )
break ;
}
 
if ( i == n )
printf ( “Number is not present in the array.” ) ;
else
printf ( “The number is at position %d in the array.”, i ) ;
 
getch( ) ;
}

Output :

Leave a Comment

Your email address will not be published.

Shopping Basket
Verified by MonsterInsights