Martes, Oktubre 16, 2012

Write a simple code for linear search in c programming language.

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

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


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


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


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


printf("Enter the number to be search: ");
scanf("%d",&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; }

Walang komento:

Mag-post ng isang Komento