Wednesday, 3 September 2014

search an element in a 1d array

#include<stdio.h>
#include<conio.h>
int main()
{
    int list[100],n,x,i,p;
    printf("\n enter the size of list");
    scanf("%d",&n);
    printf("\n enter the elements");
    for(i=0;i<n;i++)
    scanf("%d",&list[i]);
    p=0;
    printf("\n enter the element to be search");
    scanf("%d",&x);
    for(i=0;i<n;i++)
    {
                    if(list[i]==x)
                    p=i;
    }
    if(p>0)
    {
           printf("\nthe element %d is at position %d",x,p+1);
    }
    else
    {
        printf("\n wcta");
    }
getch();
return 0;
}
output:
enter the size of the list3

enter the elements10
30
20

enter the element to be search30

the element 30 is at position 2



No comments:

Post a Comment

Note: only a member of this blog may post a comment.