// Program to illustrate Linear Search
#include <stdio.h>
int main()
{
int array[100], search, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("\n Array elements are :");
for (i = 0; i < n; i++)
{
printf("\n Address :%p --> Array : array[%d] Data : %d Location : %d", &array[i],i,array[i],i+1);
}
printf("\nEnter a number to search :");
scanf("%d", &search);
for (i = 0; i < n; i++)
{
if (array[i] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
output:
#include <stdio.h>
int main()
{
int array[100], search, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("\n Array elements are :");
for (i = 0; i < n; i++)
{
printf("\n Address :%p --> Array : array[%d] Data : %d Location : %d", &array[i],i,array[i],i+1);
}
printf("\nEnter a number to search :");
scanf("%d", &search);
for (i = 0; i < n; i++)
{
if (array[i] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
output:
No comments:
Post a Comment