C++ Program to locate a no. in a list

//To locate a number in a list 
#include <iostream> 
using namespace std; 
int main() 
{     
    int i,n;         
    double x;     
    cout<<"Enter the size of the list"<<endl; 
    cin>>n;                    //Input the size of the list 
    double a[n];                //declare an array of the size entered by the user 
    cout<<"Enter the elements of the list\n"; 
    for (i=0;i<n;i++)            //loop to input the elements of the list 
    { 
    cin>>a[i]; 
    } 
    cout<<"Which no. do you wish to find?\n";     
    cin>>x;                    //Input the no. to be searched 
    for(i=0;i<n;i++)            //loop to search the no. 
    { 
        if (a[i]==x)            //compare each element with the no. to be searched 
        { 
        cout<<"The no. is at "<<i+1<<"th position in the list.\n";//display the position 
        break;                            //break the loop 
        } 
        if(i==n-1)                         
        cout<<"Sorry, the no. can't be found\n"; 
    } 
    return 0;                //end program 
}

[wpedon id="7041" align="center"]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.