Linear Search Algorithm in C

Get Certified in C Programming and Take Your Skills to the Next Level

Program 1

// Program for find max element from  Array
#include<stdio.h>
#include<conio.h>
int main()
{
        int a[500],n,i,max,p;
        system("cls");
        xyz: printf("\nEnter the limit of array");
        scanf("%d",&n);
        if(n<0 || n>500)
       { 
          printf("\nInvalid limit please enter again)");
           goto xyz;
       }
       else
       {
            printf("\n Enter elements in array");
            for(i=0;i<n;i++)
              scanf("%d",&a[i]);

                 max=a[0];
                 p=0;
             for(i=1;i<n;i++)    
             {
                   if(a[i]>max)
                   {
                       max=a[i];
                       p=i;
                   }
             }
           printf("\nLargest element is %d ",max);
           printf("Position of element is %d ",p);     
       }
}

Program 2

// Program for linear search
#include<stdio.h>
#include<conio.h>
int main()
{
        int a[500],n,i,s,f=0;
        system("cls");
        xyz: printf("\nEnter the limit of array");
        scanf("%d",&n);
        if(n<0 || n>500)
       { 
          printf("\nInvalid limit please enter again)");
           goto xyz;
       }
       else
       {
            printf("\n Enter elements in array");
            for(i=0;i<n;i++)
              scanf("%d",&a[i]);
            printf("\n Enter an element for search");
            scanf("%d",&s);
            for(i=0;i<n;i++)
            {
                   if(a[i]==s)
                     {
                        f=1;
                        break;
                     }
            }
            if(f==1)
              printf("Searching success");
            else
               printf("Searching not success"); 
       }
}

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *