Size of array is taken from user.. Elements are entered.. Then user is asked to enter element to be searched.. Search is performed..
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int ar[10],n,num,no;
- cout<<"Enter size of the array: ";
- cin>>n;
- cout<<"Enter array element: "<<endl;
- for(int i=0;i<n;i++)
- {
- cout<<"Enter element "<<i<<": ";
- cin>>ar[i];
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- cout<<"Elements of array: "<<endl;
- for(i=0;i<n;i++)
- cout<<"Element at index number "<<i<<" is: "<<ar[i]<<endl;
- //////To search////
- cout<<"Enter number to search: ";
- cin>>num;
- for(i=0;i<n;i++)
- {
- if(num==ar[i])
- {
- cout<<"Found at index number: "<<i;
- no=0;
- break;
- }
- else
- {
- no=1;
- continue;
- }
- }
- if(no==1)
- cout<<"Sorry your search returned NO results. :(";
- getch();
- }