C Program to Search a Given Number Using Linear Search


Couch ModePrint

PROGRAM:

#include<stdio.h>
main()
{
   int nums[40];
   int key,i,n,location=0;
   clrscr();
   printf("Enter the size of an array:\n");
   scanf("%d",&n);
   printf("Enter the array element:\n");
   for(i=0;i<n;i++)
   scanf("%d",&nums[i]);
   printf("Enter the key element:");
   scanf("%d",&key);
   for(i=0;i<n;i++)
   if(nums[i]==key)
     {
        location=i+1;
        break;
     }
   if(location==0)
     {
        printf("The given key element doesn't exist and is not found");
     }
   else
   printf("The given key element %d exists and is found %d position",key,location);
   getch();

}

OUTPUT:

Enter the size of an array:
6
Enter the array element:
22
55
99
87
66
01
Enter the key element: 99
The given key element 99 exists and is found 3 position.


Enter the size of an array:
6                                                  
Enter the array element:                                                                               
101                                                
45                                                 
23                                                  
30                                                 
69                                                 
72                                                 
Enter the key element: 10                           
The given key element doesn't exist and is not found


Thank Your Valuable Comment Tool to Insert codes in Comments : Convert My Code. Hide

Select to parse the code (not required if code is already parsed):

Subscribe By Email

Join Our Newsletter

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!

We Respect Your Privacy

Popular Posts