C

Linear search algorithm in C

#include <stdio.h>

int main() {
    int n,search,i;
    int arr[100];
    printf("How many number you want to enter!\n");
    scanf("%d",&n);
    printf("Please enter the numbers\n");
    for(i = 0; i<n; i++){
        scanf("%d",&arr[i]);
    }


    printf("The number you want to search\n");
    scanf("%d",&search);

    for (int j = 0; j < n; ++j) {
        if (arr[j] == search){
            printf(" found %d at %d", arr[j], j+1);
            break;
        } else{
            printf("not found");
            break;
        }
    }
    return 0;
}

Leave a Reply

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

October 2020
M T W T F S S
« Sep   Nov »
 1234
567891011
12131415161718
19202122232425
262728293031  

Site Statistics

  • Users online: 0 
  • Visitors today : 0
  • Page views today : 0
  • Total visitors : 3,982
  • Total page view: 4,457