A program to search an element from a single dimensional array (LINEAR SEARCH)
C Programming Language 052
The following code shows how to write a program to search an element from a single dimensional array (LINEAR SEARCH).
Program
#include <stdio.h>
void main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array: ");
scanf("%d", &number);
printf("Enter %d numbers:\n", number);
for (c = 0; c < number; c++)
{
scanf("%d", &array[c]);
}
printf("Enter the number to search: ");
scanf("%d", &search);
for (c = 0; c < number; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d\n", search, c + 1);
break;
}
}
if (c == number)
{
printf("%d is not present in array\n", search);
}
printf("\n");
}
Result
Enter the number of elements in array: 4
Enter 4 numbers:
10
30
25
54
Enter the number to search: 30
30 is present at location 2
Last Updated on
Comments