‘C’ program to search for a specific number in array.

C File =>


/* Write a ‘C’ program to accept n integers in an array and search for a  specific number. */

#include<stdio.h>
int main() 
{
       int n,i,key,arr[30],flag=0;
   
       printf("\n How many integers? ");
       scanf("%d",&n);
 
       for(i=0;i<n;i++)
      {
         printf("\n Enter element%d",i+1);
         scanf("%d",&arr[i]);
       }
         printf("\n Enter element to search : ");
         scanf("%d",&key);
 
        // Searching for key
        for(i=0;i<n;i++)
       {
            if(arr[i]==key)
           {
               flag=1;
               break;
            }
        }

      if(flag==1)
          printf("\n %d is found in array",key);
     else
          printf("\n %d is Not found in array",key);
 
   return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.