Linear search in C++ Program Example Code

Levels of difficulty: / perform operation:

Linear search or sequential search is one of the searching algorithm in which we have some data in a data structure like array data structure and we have to search a particular element in it which is know as key.
By traversing the whole data structure elements from start to end one by one to find key comparing with each data structure element to the key.

In case of an array we check that the given key or a number is present in array at any index or not by comparing each element of array

There can be two possible outcomes if we are assuming that data structure like array contains unique values.

Linear search successful Key Found means in array at an index we found value which matches to our key
Linear search failed to find the Key mean our key does not exist in data

C++ Program

using namespace std;
int main() {
	cout<<"Enter The Size Of Array:   ";
	int size;
	int array[size], key,i;
	// Taking Input In Array
	for (int j=0;j<size;j++) {
		cout<<"Enter "<<j<<" Element: ";
	//Your Entered Array Is
	for (int a=0;a<size;a++) {
		cout<<"array[ "<<a<<" ]  =  ";
	cout<<"Enter Key To Search  in Array";
	for (i=0;i<size;i++) {
		if(key==array[i]) {
			cout<<"Key Found At Index Number :  "<<i<<endl;
	if(i != size) {
		cout<<"KEY FOUND at index :  "<<i;
	} else {
		cout<<"KEY NOT FOUND in Array  ";
	return 0;