/*Linear search program in c*/
Below program will print all the possible locations at which entered element is found and also the how many number of times it occur in the list.
Simple Linear Search in C
Linear search using function in C
Linear search function using pointers in C
Below program will print all the possible locations at which entered element is found and also the how many number of times it occur in the list.
#include <stdio.h>
int main()
{
int array[100], search, c, n, count = 0;
printf("Please enter number of elements in array\n");
scanf("%d", &n);
printf("please enter %d numbers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Please enter number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++) {
if (array[c] == search) {
printf("%d is present at location %d.\n", search, c+1);
count++;
}
}
if (count == 0)
printf("%d is not present in array.\n", search);
else
printf("%d is present %d times in array.\n", search, count);
return 0;
}
int main()
{
int array[100], search, c, n, count = 0;
printf("Please enter number of elements in array\n");
scanf("%d", &n);
printf("please enter %d numbers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Please enter number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++) {
if (array[c] == search) {
printf("%d is present at location %d.\n", search, c+1);
count++;
}
}
if (count == 0)
printf("%d is not present in array.\n", search);
else
printf("%d is present %d times in array.\n", search, count);
return 0;
}
Output:
Please enter number of elements in array
7
Please enter 7 numbers
1
3
4
3
3
5
8
Please enter number to search
3
3 is present at location 2.
3 is present at location 4.
3 is present at location 5.
3 is present 3 times in array.
Read More:Please enter number of elements in array
7
Please enter 7 numbers
1
3
4
3
3
5
8
Please enter number to search
3
3 is present at location 2.
3 is present at location 4.
3 is present at location 5.
3 is present 3 times in array.
Simple Linear Search in C
Linear search using function in C
Linear search function using pointers in C
0 Comments