Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Linear search in C++

The following code implements linear search (Searching algorithm) which is used to find whether a given number is present in an array and if it is present then at what location it occurs. It is also known as sequential search. It is very simple and works as follows: We keep on comparing each element with the element to search until the desired element is found

Source Code:-

#include <iostream.h>
#include <conio.h>
void main()
{  clrscr();
   int array[100], search, c, n;
   cout<<"Program for Linear Search by-Tarun Rawat\n";
   cout<<"Enter the number of elements in array :";
   cin>>n;
   cout<<"Enter "<<n<<" integer\n";
   for (c = 0; c < n; c++)
      cin>>array[c];
   cout<<"Enter the number to search\n";
   cin>>search;
   for (c = 0; c < n; c++)
   { if (array[c] == search)     /* if required element found */
      {  cout<<search<<" is present at location "<< c+1;
      }
   }
   if (c == n)
      cout<<search<<" is not present in array.\n";
getch();

}

No comments:

Post a Comment

Blogger Widgets

Find Us On Google, Just type - way2cplusplus -

If you have any questions on implementing or understanding this C++ Program , shoot me a comment and I'll be glad to help! :) :)