Linear Search C Program

Write a simple code for linear search in c programming language

Write a c program to search an element in an array using linear search

#include<stdio.h>
int main(){

int a[10],i,n,m,c=0;

printf("Enter the size of an array: ");
scanf("%d",&amp;n);

printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++){
scanf("%d",&amp;a[i]);
}

printf("Enter the number to be search: ");
scanf("%d",&amp;m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");

return 0;
}

Output:

Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found

Tags:

Comments

Leave a Reply