Home

A program to search an element in an array using pointers

C Programming Language 104

Program

#include <stdio.h>

void main()
{
    int a[30], n, i, *p, key;

    printf("Enter how many numbers you want to input: ");
    scanf("%d", &n);

    p = a;
    printf("\nEnter numbers:\n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", p + i);
    }

    printf("Enter the element to be searched: ");
    scanf("%d", &key);

    for (i = 0; i < n; i++)
    {
        if (key == (*(p + i)))
        {
            printf("Number found at %d", i + 1);
        }
    }

    printf("\n");
}

Result

Enter how many numbers you want to input: 3

Enter numbers:
1 2 3
Enter the element to be searched: 2
Number found at 2


Last Updated on

Next Post: Rules vs Guidelines →

Comments