C Program to Search an Element in Array

Below is a simple C program that searches for an element in an array and prints the index if the element is found or a message indicating that the element is not present .

Program :

#include <stdio.h>
 #include <conio.h>
 
 
int main()
{
    int i,n,key;
   
    printf(“Enter size of the  array : “);
    scanf(“%d”, &n);
    int arr[n];
    printf(“Enter elements in array : “);
    for(i=0; i<n; i++)
    {
        scanf(“%d”,&arr[i]);
    }
    printf(“Enter the key : “);
    scanf(“%d”, &key);
     
    for(i=0; i<n; i++)
    {
        if(arr[i]==key)
        {
printf(“element found “);
            return 0;  
        }
    }
 
printf(“element  not  found”);
}

Output :

Leave a Comment

Your email address will not be published.

Shopping Basket
Verified by MonsterInsights