LINEAR SEARCH USING C PROGRAM





1. Write a simple code for linear search in c programming language
2. Wap a c program to search an element in an array using linear search

#include<stdio.h>
int main(){

    int a[10],i,n,m,c=0;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements of the array: ");
    for(i=0;i<=n-1;i++){
         scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);
    for(i=0;i<=n-1;i++){
         if(a[i]==m){
             c=1;
             break;
         }
    }
    if(c==0)
         printf("The number is not in the list");
    else
         printf("The number is found");

    return 0;
}

Sample output:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found





44 comments:

kohila said...

simple & clear program

Anonymous said...

why the hell have u used break;

Psylo said...

How to Explain People about searching linear in array?

Anonymous said...

I hate programming. But this program is actually understandable

manish said...

I am used break keyword to reduce the time complexity of program. If break keyword will not present then loop will continue even searching element has already found .

nagendra said...

really appreciated,liked it,but where is header file............

peeyush said...

good logic

Anonymous said...

is it running??

ashwath said...

wow hw clever u using break.....bt wat the f*** i don get it...

leonidas said...

its good

nikita said...

really simple to understand..

Satishan36 said...

hey bro, plz tell whether this can be shorten by using function calls??

Anonymous said...

can i get the prg using dynamic memory alllocation

Chavali's Adda said...

program is not runnig dear.....

Anonymous said...

program is running clear

gautham said...

Thanx :)

Anu said...

this is an example of a good programer

sasi said...

thank u very much

VIDHYA,SHARI,REMYA said...

ITS VERY SIMPLE AND UNDERSTANDABLE ONE
GOOD!!!!!!!!!!!!!!

senthil said...

if i give two same input and i need to print the memory location of the values will u plz send mr that prog

Anonymous said...

why are u confusing with
for(i=0;i<=n-1;i++),
we can write
for(i=1;i<=n;i++)

viggi47 said...

explan:-
because if n starts with 0 the last element should be n-1
if we initialize i with 1 then last element n.
suppose
n=5;
i=0;
alloted memories are 0,1,2,3,4. 5 terms
the last element is 4 i.e, equal to 5-1=4 => n-1.

Anonymous said...

Write a function, Iserach, which accepts the following parameters. arr, an array of integers
n, the amount of elements in arr, and
key, the integer being searched for

and performs a linear serach on arr for key . if key is found, your function should return the location in the array arr, where it was found. otherwise, it should return-1.

pasha said...

to get the position of the element found we should mention the position
so mention the position also.

Anonymous said...

can we use continue in place of break?

soni khati said...

flowchart of linear search

Le Thanh Dat said...

can i possible return 0; = return 1; ???help me,please!

kunal goswami said...

thnkzz mam .....ur program is helpful for any 1.......

Anonymous said...

superb program

Anonymous said...

What is the use of return 0;

Anurag said...

u r doing very good work bro sreisly. Keep it up

Jack Hack said...

tried..not working :(

prasanth said...

its easy to understand... but what if an element is present more than once in the array....

Surya Pradhan said...

prgrm nut shwng crc8 out put :p :p nd guyz dunt cpy nd past diz prgrm.....

Rezaul Alam said...

Q) Write a program to identify keyword in 'c'
sample input: for

sample output: this is keyword

sample input: why

sample output: this is not keyword

Manav Sharma said...

what is c variable in code? please explain from if conditions

Unknown said...

working perfectly

PRASHANT KAMBLE said...

if the element is found, there is no need to check for remaining positioned elements

cheers4suraj said...

c variable gets assigned 1 if the element exists. else it prints the statement element not found. , you could also assign c=i, and as the i value will be the location of the element in the array. and print c value.

imran hossen said...

Right... i agree with you. This was my question also..

Nischal K.P said...

how to calculate time complexity?? means the best and the worst case of the execution??

mostafa ihab said...

example
Pie Sequence

mostafa ihab said...

example Pie Sequence

Raja vikrant Sharma said...

If there are two same numbers