C++ or C program for Linear Search

File Name:- ls.cpp

#include<iostream.h>
#include<conio.h>
#define N 5
void ls(){
int list[N],item,i;
clrscr();
cout<<"Enter Elements of List"<<endl;
for(i=0;i<N;i++){
cout<<"Enter"<<i+1<<"th element=";
cin>>list[i];
}
cout<<"Enter Item to search=";
cin>>item;
for(i=0;i<N;i++){
if(item==list[i]){
cout<<item<<" is found at "<<i<<" index  and "<<i+1<<" position"<<endl;
return;
}
}
cout<<item<<" is not found"<<endl<<"Search Unsuccessful"<<endl;
return;
}
void main(){
clrscr();
cout<<"Linear Search"<<endl;
ls();
getch();
}


File Name:- ls.c

#include<stdio.h>
#include<conio.h>
#define N 5
void ls(){
int list[N],item,i;
clrscr();
printf("Enter Elements of List\n");
for(i=0;i<N;i++){
printf("Enter %d element=",i+1);
scanf("%d",&list[i]);
}
printf("Enter Item to search=");
scanf("%d",&item);
for(i=0;i<N;i++){
if(item==list[i]){
printf("%d is found at %d index and %d position\n",item,i,i+1);
return;
}
}
printf("%d is not found\n Search Unsuccessful\n",item);
return;
}
void main(){
clrscr();
printf("Linear Search\n");
ls();
getch();
}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...