Wednesday 11 January 2012

Write a c program to search the key value in a set of n values

#include<stdio.h>
#include<conio.h>

void main()
{
    int a[10],n,i,key,flag=0,pos;
    clrscr();
    printf("How many numbers you want to insert?\n");
    scanf("%d",&n);
    printf("Enter the numbers::\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter the key value you want to search::\n");
    scanf("%d",&key);
    for(i=0;i<n;i++)
    {
        if(key==a[i])
        {
            flag=1;
            pos=i+1;
            break;
        }
    }
    if(flag==1)
    printf("%d is found in %d position in the array",key,i+1);
    else
    printf("Key doesnot exist");
    getch();
}

No comments:

Post a Comment