Linear Search coderinme

Linear Search coderinme

Linear Search coderinme
Let us now write a program in C to search the number using linear search

#include<stdio.h>
int lin_search(int list[], int size, int tar)
{
int pos=0;
while(pos < size && list[pos] != tar)
pos++;
if(list[pos] == tar)
return pos;
else
return -1;
}
int main( )
{
int size, num, pos, i, list[20];
do
{
printf("\nEnter size of list(array): ");
scanf("%d", &size);
if(size < 1 || size > 20)
printf("\nPLEASE GIVE VALID VALUE AND TRY AGIAN\n");
}
while(size < 1 || size > 20);
for(i=0; i < size; i++)
{
printf ("Enter %d elements of the list: ", i);
scanf("%d", &list[i]);
}
printf("Please Input value which you want to search: ");
scanf("%d", &num);
pos = lin_search(list,size,num);
if(pos != -1)
{
printf("\nThe location of number %d in the given list is: %d\n", num,pos + 1);
}
else
printf("\nNumber %d not found\n",num);
getch();
return 0;
}

For more programs in C, visit our C Coder Archives

Linear Search coderinme

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.