How to search element in an array.

Let’s write a program to search number in Array.

 

Step 1: Let’s open turbo c/c++ and write the following code .

 

\Add preprocessor

 

#include<stdio.h>

#include<conio.h>

void main()

{

// Declare variable

     long  int a[5],i,element,f=0;

//Clear screen

       clrscr();

     printf(“provide input to an array : ”);

for(i=0;i<5;i++)

{

scanf(“%li”,&a[i]);

}

printf(“\n enter the element to be search in an array”);

scanf(“%li”,&element);

for(i=0;i<5;i++)

{

if(a[i]==element)

{

f=1;

break;

}

}

if(f==1)

{

printf(“\n number is present in an array”);

}

else

{

printf(“\n number is not present in an array”);

}

getch();

}

 

 

Step 2: Now save this program by prssing F2 ,and name it FINDNUMBER.CPP.

 

 

 

Step 3: After save it, Compile it by pressing ALT+F9 to check errors and correct them.

 

 

Compile

 

 

Step 4: Now run the program by click on Run or by pressing ALT+R and see output.

 

 

Output :

 

 

Output

 

 

Happy coding.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Tags: ,