recent

C Program to Search an element in Array

C Program to Search an element in Array

#include<stdio.h>

int main()
{

   int a[20], element, num, i;

   printf("Enter no of elements :\n");
   scanf("%d", &num);  //input from user

   printf("Enter the values :\n");
   for (i = 0; i < num; i++)
   {
      scanf("%d", &a[i]);
   }
   printf("Enter the elements to be searched :\n");
   scanf("%d", &element);   //input from user

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

   //If i < num then Match found
   if (i < num)
   {
    printf("Number found at the location = %d\n", i + 1);   //print output on screen
   }
   else
   {
      printf("Number not found\n");   //print output on screen
   }

}
Powered by Blogger.