Tuesday, 2 August 2016

Arrays Linear Search

Search for key Element in an Array using Linear Search

Download the Program

void main()
    int i, j, n, a[100], key;
    printf("\nEnter value of n:");
    scanf("%d", &n);
    printf("\nEnter array elements:");
    for(i=0; i<n; i++)
        scanf("%d", &a[i]);
    printf("\nEnter key:");
    scanf("%d", &key);

    for(i=0; i<n; i++)
            if(a[i] == key)
        printf("\nKey found at position %d", i+1);
        printf("\nNot found");

Enter value of n: 6
Enter array elements: 1  3  2  4  5  1
Enter key: 3
Key found at position 2

No comments:

Post a Comment