-->

WAP to search an element stored in an array.



using turbo c++
----------------------------------------------------------------------------------------------------------
// program to search an element in an array

#include <stdio.h>
#include<conio.h>
void main()
{
    int a[100],n,i,number,location,j,found;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers before search, are:\n");
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
    printf("enter number  to be searched\n");
    scanf("%d",&number);
    for(i=0;i<n;i++)
    {
        if(number==a[i])
        {
            found=0;
            break;
        }
        else
        {
            found=1;
        }
    }
    if(found==0)
    {
    printf("number found and number %d location is %d\n",number,i);
    }
    else
    {
        printf("given number is not in the list");
    }
    getch();
}

-----------------------------------------------------------------------------------------------------------------
using codeblocks/dev c++/online compiler
----------------------------------------------------------------------------------------------------------------
// program to search an element in an array

#include <stdio.h>

int main()
{
    int a[100],n,i,number,location,j,found;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers before search, are:\n");
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
    printf("enter number's  to be searched\n");
    scanf("%d",&number);
    for(i=0;i<n;i++)
    {
        if(number==a[i])
        {
            found=0;
            break;
        }
        else
        {
            found=1;
        }
    }
    if(found==0)
    {
    printf("number found and number %d location is %d\n",number,i);
    }
    else
    {
        printf("given number is not in the list");
    }
    return 0;

}

No comments:

Post a Comment