Whatsapp

Linear Search in C language


#include<conio.h>
#include<stdio.h>
int linear_search(int arr[],int size,int item){
    int i,j;
    for(i=0;i<size;i++){
                        for(j=0;j<size;j++){
                                            if(arr[j]==item){
                                                             return j;
                                                               }
                                            }
                        }
                        return -1;
}
int main(){
    int size,search,item,i,arr[50];
    int a=1;
    printf("Enter the size of array : ");
    scanf("%d",&size);
    printf("\nEnter the elements for the array with spaces : ");
    for(i=0;i<size;i++){
                        scanf("%d",&arr[i]);
                        }
    do{
    printf("\nEnter the searchable term :");
    scanf("%d",&item);
    search=linear_search(arr,size,item);
    if(search==-1){
                   printf("No element found !\n");
                   }
    else{
         printf("\nElement found at index : %d ",search);
         }
    printf("\nEnter -1 to stop else to continue  : ");
    scanf("%d",&a);
    }while(a!=-1);
         getch();
         
    return 0;
}

Post a Comment

0 Comments

Ad Code

Responsive Advertisement