Program to Implement Linear Search in CPP (C plus plus)

Program to implement Linear Search in CPP (C plus plus)

In this tutorial, we will learn about Program to implement Linear Search in CPP (C plus plus).

#include<iostream>
 
using namespace std;
 
int main()
{
    int array[20];
	int val;
	int k;
	int i;
	int j=0;
	
    cout<<"Please enter the number of values you want to enter: ";
    cin>>val;
    
    cout<<endl<<"Please enter the values into the array"<<endl;
    
    for(i=0;i<val;++i)
        cin>>array[i];
    
    cout<<endl<<"Please enter value to search: ";
    cin>>k;
    
    for(i=0;i<val;++i)
    {
        if(array[i]==k)
        {
            j=1;
            break;
        }
    }
    
    if(j)
        cout<<endl<<"Value found at position "<<i+1;
    else
        cout<<endl<<"Value not found";
        
    return 0;
}

Output:

Program to implement Linear Search in CPP (C plus plus)
Program to implement Linear Search in CPP (C plus plus)
Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.