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





33 comments:

  1. why the hell have u used break;

    ReplyDelete
  2. How to Explain People about searching linear in array?

    ReplyDelete
  3. I hate programming. But this program is actually understandable

    ReplyDelete
  4. 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 .

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

    ReplyDelete
  6. is it running??

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

    ReplyDelete
  8. really simple to understand..

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

    ReplyDelete
  10. can i get the prg using dynamic memory alllocation

    ReplyDelete
  11. program is not runnig dear.....

    ReplyDelete
  12. program is running clear

    ReplyDelete
  13. this is an example of a good programer

    ReplyDelete
  14. VIDHYA,SHARI,REMYA6/27/11, 3:48 PM

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

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

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

    ReplyDelete
  17. 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.

    ReplyDelete
  18. 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.

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

    ReplyDelete
  20. can we use continue in place of break?

    ReplyDelete
  21. flowchart of linear search

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

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

    ReplyDelete
  23. superb program

    ReplyDelete
  24. What is the use of return 0;

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

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

    ReplyDelete