function searchValue(value, target)
{
for (var i = 0; i < value.length; i++)
{
if (value[i] == target)
{
return i;
}
}
return -1;
}
searchValue([10, 5, 15, 20, 25, 35] , 25); // Call the function with array and number to be searched
#include <stdio.h>
int main()
{
int arr[50], search, cnt, num;
printf("Enter the number of elements in array\n");
scanf("%d",&num);
printf("Enter %d integer(s)\n", num);
for (cnt = 0; cnt < num; cnt++)
scanf("%d", &arr[cnt]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (cnt = 0; cnt < num; cnt++)
{
if (arr[cnt] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, cnt+1);
break;
}
}
if (cnt == num)
printf("%d is not present in array.\n", search);
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int f, l, m, size, i, sElement, list[50]; //int f, l ,m : First, Last, Middle
clrscr();
printf("Enter the size of the list: ");
scanf("%d",&size);
printf("Enter %d integer values : \n", size);
for (i = 0; i < size; i++)
scanf("%d",&list[i]);
printf("Enter value to be search: ");
scanf("%d", &sElement);
f = 0;
l = size - 1;
m = (f+l)/2;
while (f <= l) {
if (list[m] < sElement)
f = m + 1;
else if (list[m] == sElement) {
printf("Element found at index %d.\n",m);
break;
}
else
l = m - 1;
m = (f + l)/2;
}
if (f > l)
printf("Element Not found in the list.");
getch();
}