C++ PRACTICAL PROGRAMS

Class 12 Computer Science | ASSEB

Program to search an element in an array using Linear Search

#include<iostream>

using namespace std;

int main( )

{

int a[50], n, key, pos= -1;

cout<<”Enter number of elements”;

cin>>n;

cout<<”Enter array elements”;

for( int i=0; i<n; i++)

{

cin>>a[i];

}

cout<<”\n Enter element to be searched”;

cin>>key;

for( int i=0; i<n; i++)

 {

if (a[i] == key) 

{

pos=i;

break;

}

}

if (pos == -1)

cout<<”Element not found”;

else

cout<<”Search successful. Element found at position ”<<pos+1;

return 0;

}

Output

Enter number of elements 5

Enter array elements 8

4

6

2

9

Enter element to be searched 6

Search successful. Element found at position 3

QUICK LINKS

Class 11

Class 12

Previous Year Paper

Practical Programs

Scroll to Top