IT HOME Programming care

IT HOME Programming Care

Sunday, January 29, 2017

Linear Search in C Programming

Linear Search in C Programming http://ithomeprogrammingcare.blogspot.com/2017/01/linear-search-in-c-programming.html
Linear Search in C Programming




//Linear Search in C Programming:
#include<stdio.h>
int main()
{
int i,location=-1,n;
printf("Enter the number of total series: ");
scanf("%d",&n);
int data[n];
printf("Enter the %d values:  ",n);
for(i=0;i<n;i++)
{
    //series value input
    scanf("%d",&data[i]);
    //ask next value
    if(i<n-1)
    printf("\nEnter the next value: ");
}
    int key;
    printf("\nPress the value you want to Find: ");
    scanf("%d",&key);
    i=0;
    while(i<n)
    {
        if(key==data[i]){
            location=i+1;
            printf("\nThe value=%d is in location=%d \n",key,location);
            break;
        }
        i++;
    }
    if(location==-1){
        printf("\nItem is not in the list");
    }
    return 0;
}



//solution-2: Linear Search with inputted value

//Linear Search in C Programming:
#include<stdio.h>
int main()
{
int k=0,loc=-1,data[5]={222,11,1222,13,24};
int key;
printf("\nPress the Finding Value: ");
scanf("%d",&key);
while(k<5)
{
if(key==data[k])
{
loc=k+1;
printf("\nloc=%d and key=%d \n",loc,key);
break;
}
k++;
}

if(loc==-1)
{
printf("\nItem is not in list");
}
return 0;
}

0 comments:

Post a Comment

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();

IT HOME Freelancing Care