Searching in Array data structure by R4R Team

Array:

Array is the collection of different values having same data type.
-We have a integer Array that having all integer values.
-Character array is known as String.

How we Search in Array?
-There are two possible way to search anything in Array's
1. Linear Search
2. Binary Search(applicable on sorted data)

program-

#include < stdio.h>
int main()
{
int a[100],n,i,num;
printf("Number of elements in array ?\n");
scanf("%d",&n);
printf("Enter %d number in array\n",n);
for(i=0;i< n;i++){
scanf("%d",&a[i]);
}
printf("Enter number to search in the array\n");
scanf("%d",&num);
for(i=0;i< n;i++)
{
if(a[i]==num){
printf("Number is at %dth position",i+1);
break;
}
}
if(i==n)
{
printf("Number is not in array");
}
}


output-

Number of elements in array ?
5
Enter 5 number in array
1 2 3 4 5
Enter number to search in the array
4
Number is at 4th position

Number of elements in array ?
5
Enter 5 number in array
1 2 3 4 5
Enter number to search in the array
6
Number is not in array

-In this program, firstly we take a input in array, and then take a input of number that we search in tha array.
-Traverse whole array and check for each value if value matched then break the loop otherwise continue until the length of the array.




Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!