Write a program for linear search

Write a program for linear search


// Write a program for linear search
// Ex: [4,2,6,7,3,5,8,2], 2 -> found at index 1, 9 -> not found
// Author: AimForJob.com
#include
#include
int main(){
     int len,i=0,item;
     printf("Enter length of an array:");
     scanf("%d",&len);                 // array length input from user
     int array[len];
    
     // user input for matrix1 elements
     printf("Enter Matrix1 Elements");
     for(i=0;i<len;i++){
             scanf("%d",&array[i]);
     }
 
     printf("Enter the Element you want to search in Array: ");
     scanf("%d",&item);
     for(i=0;i<len;i++){
         if(array[i]==item){
              printf("Element found at Index: %d",i);
              exit(1);
         }  
     }
     printf("Element not found in the array");
}

No comments:

Post a Comment