Write a program to do linear search for an integer in 20 distinct numbers. The program should return the location of an element – IGNOU BCA Assignment 2015 – 16

By | October 8, 2015

BACHELOR OF COMPUTER APPLICATIONS

Course Code : BCSL-045
Course Title : Introduction to Algorithm Design Lab
Assignment Number : BCA(IV)/L-045/Assignment/2015
Maximum Marks : 50
Weightage : 25%

Write a program to do linear search for an integer number in an array of 20 distinct numbers. The program should return the location of an element if the number found in the array.

#include<stdio.h>
void main()
{
int I,Pos,KEY,TEMP,N=20,flag=0;
int List[]={14,6,10,3,12,19,4,20,8,11,2,18,15,5,13,9,17,1,16,7};
clrscr();
printf(“\n\nList to be searched :\n\n”);
for(I=0;I<N;I++)
{
printf(“%d “,List[I]);
}
printf(“\n\nEnter Element to SEARCH : “);
scanf(“%d”,&KEY);
for(I=0;I<N;I++)
{
if(List[I]==KEY)
{
flag=1;
Pos=I;
}
}
if(flag==1)
printf(“\n\nElement %d is found at Location Number %d.”,KEY,Pos+1);
else
printf(“\n\nKEY NOT FOUND !!!”);
getch();
}

Code:

[codesyntax lang=”php”]

#include<stdio.h>
void main()
{
int I,Pos,KEY,TEMP,N=20,flag=0;
int List[]={14,6,10,3,12,19,4,20,8,11,2,18,15,5,13,9,17,1,16,7};
clrscr();
printf("\n\nList to be searched :\n\n");
for(I=0;I<N;I++)
{
printf("%d ",List[I]);
}
printf("\n\nEnter Element to SEARCH : ");
scanf("%d",&KEY);
for(I=0;I<N;I++)
{
if(List[I]==KEY)
{
flag=1;
Pos=I;
}
}
if(flag==1)
printf("\n\nElement %d is found at Location Number %d.",KEY,Pos+1);
else
printf("\n\nKEY NOT FOUND !!!");
getch();
}

[/codesyntax]

ScreenShots:

C_program_Linear_Search

C_program_Linear_Search_Output