Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Thursday, 24 October 2013

[C Program] to Implement Linear Search


//Program for Linear Search
#include<stdio.h>
#include<conio.h>

//Main Function
void main()
{
int a[5],i,n,key,flag=0;
clrscr();
printf("\nEnter the array size:");
scanf("%d",&n);
printf("\nEnter the elements into array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the element to be searched:");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("\nElement is present in the list at %d location.",i);
flag=1;
}
}
if(flag==0)
{
printf("\nElement is not present in the list.");
}
getch();
}//end of main

/*Output

Enter the array size:5                                                        
                                                                               
Enter the elements into array:                                                
1 2 6 4 8                                                                      
                                                                               
Enter the element to be searched:2                                            
                                                                               
Element is present in the list at 1 location. */
Share it Please

Pankaj Gaikar

Pankaj Gaikar is a professional blogger from Pune, India who writes on Technology, Android, Gadgets, social media and latest tech updates at Punk Tech , Being Android & Shake The Tech . Email me HERE

0 comments :

Post a Comment

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine