C Program to Search Element from Array

C Program to Search Element from Array:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>

main()
{
   int a[50],ele,num,i;

   printf("Enter no of elements: ");
   scanf("%d",&num);

   printf("\nEnter %d Elements: ",num);
   for(i=0;i<num;i++)
    {
      scanf("%d",&a[i]);
    }

   //Accepting element to be search
   printf("\nEnter the element to search: ");
   scanf("%d",&ele);

   i=0;
   while(i<num && ele!=a[i])
    {
      i++;
    }

   if(i < num)
    {
      printf("Number found at location: %d",i+1);
    }
   else
    {
      printf("Number not found");
    }
}

OUTPUT:

Enter no of elements: 10

Enter 10 Elements: 7 1 3 9 52 14 78 22 14 37

Enter the element to search: 14
Number found at location: 6

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.