Here I am going to explain Program to implement sequential search / Linear Search in C Language
/*
Program to implement sequential search
*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 10 //Limit for Search list
int main(void)
{
int array[MAX],index=0,search_element,count=0;
clrscr();
printf("\nEnter the search list(10 elements):\n");
for(index=0;index<10;index++)
{
scanf("%d",&array[index]);
}
printf("\nEnter the element to be searched:");
scanf("%d",&search_element);
for(index=0;index<10;index++)
{
if(array[index]==search_element)
{
printf("\nElement %d found at position %d",array[index],index+1);
//exit(0);
count++;
}
}
if(count==0)
printf("\nElement %d is not present",search_element);
getch();
}
/*
Program to implement sequential search
*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 10 //Limit for Search list
int main(void)
{
int array[MAX],index=0,search_element,count=0;
clrscr();
printf("\nEnter the search list(10 elements):\n");
for(index=0;index<10;index++)
{
scanf("%d",&array[index]);
}
printf("\nEnter the element to be searched:");
scanf("%d",&search_element);
for(index=0;index<10;index++)
{
if(array[index]==search_element)
{
printf("\nElement %d found at position %d",array[index],index+1);
//exit(0);
count++;
}
}
if(count==0)
printf("\nElement %d is not present",search_element);
getch();
}
No comments:
Post a Comment