Wednesday, 3 September 2014

/*SEARCH A ELEMENT IN  ARRAY*/
#include<stdio.h>
#include<conio.h>
int main()
{
    int a[50],n,x,i,p=0;//change the coding list[100]->a[50],p->p[0]
    printf("enter the array limit:");
    scanf("%d",&n);
    printf("\nenter array elements:");
    for(i=0;i<n;i++)//insert array element
    scanf("%d",&a[i]);
    printf("\nenter the element to be search:");//filled the blank by search
    scanf("%d",&x);
    for(i=0;i<n;i++)//search the element
    {
       if(a[i]==x)
        p=i;
    }
    if(p>0)
     printf("\n%d element is found at%d positin in the array.",x,p+1);//filled blank by %d and %d pisition in the array and also modify code p->p+1
     else
      printf("%d is not present in the array.");
      return 0;
      }
      /* output:
                enter the array limit:6
                enter the array elements: 5 4 7 6 3 2
                enter the element to  be search:7
                7 element is found at 3 position in the array.
      */
      /*another logic*/
      int main()
      {
          int a[50],n,x,i,p=0,t=0;
          printf("enter the array limit:");
          scanf("%d",&n);
          printf("\n enter array elements:");
          for(i=0;i<n;i++)
           scanf("%d",&a[i]);
           printf("enter the element to be search:");
           scanf("%d",&x);
           for(i=0;i<n;i++)
           {
              if(a[i]==x)
              {
                t=1;
                 p=i;
                 }
           }
           if(t==1)
           printf("%d is found at %d element",x,p+1);
           else
           printf("%d is not found",x);
           
        reurn 0;
        }

1 comment:


  1. /* final submitation*/
    #include
    #include
    int main()
    {
    int a[50],n,x,i,p=0;//change the coding list[100]->a[50],p->p[0]
    printf("enter the array limit:");
    scanf("%d",&n);
    printf("\nenter array elements:");
    for(i=0;i0)
    printf("\n%d element is found at%d positin in the array.",x,p+1);//filled blank by %d and %d pisition in the array and also modify code p->p+1
    else
    printf("%d is not present in the array.");
    return 0;
    }
    /* output:
    enter the array limit:6
    enter the array elements: 5 4 7 6 3 2
    enter the element to be search:7
    7 element is found at 3 position in the array.
    */

    ReplyDelete

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