0% found this document useful (0 votes)
30 views5 pages

Practical No 2 Linear Search

Uploaded by

ridashaikh215
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Practical No 2 Linear Search

Uploaded by

ridashaikh215
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

// Practical no 2 WAP to perform Linear search on numbers

#include<stdio.h>

#include<conio.h>

int main()

int key, a[100], n , i ,flag=0;

printf("Enter size for array");

scanf("%d",&n);

printf("Enter %d elements for array",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Enter key to search");

scanf("%d",&key);

for(i=0;i<n;i++)

if(key==a[i])

flag=1;

break;

if(flag==1)

{
printf("Element %d found at location %d",a[i],i);

else

printf("Element not found");

return 0;

OUTPUT :- ELEMENT FOUND:-

Enter size for array 8

Enter 8 elements for array 6 5 8 4 2 0 22 1

Enter key to search 0

Element 0 found at location 5

ELEMENT NOT FOUND:-

Enter size for array3

Enter 3 elements for array6 9 3

Enter key to search10

Element not found


// Practical no 3 WAP to perform Linear search on Strings
#include<stdio.h>

#include<string.h>

int main()

char str[20][50], s1[50];

int n, i, found=0;

printf("Enter how many string (names): ");

scanf("%d", &n);

printf("Enter %d strings:\n", n);

for(i=0; i<n; i++)

scanf("%s", str[i]);

printf("Enter a string to search: ");

scanf("%s", s1);

for(i=0; i<n; i++)

if(strcmp(s1, str[i]) == 0)

found=1;

printf("Found in row-%d\n", i+1);

}
if(found==0)

printf("Not found");

return 0;

Output:- :- ELEMENT FOUND:-

Enter how many string (names): 5

Enter 5 strings:

HATE

LIKE

SMILE

CRY

ANGER

Enter a string to search: SMILE

Found in row-3

--------------------------------

ELEMENT NOT FOUND:-

Enter how many string (names): 4

Enter 4 strings:

hindi

english

marathi

sanskrit

Enter a string to search: science

Not found

You might also like