Tuesday, December 8, 2020

FInd index of an element in array.

 Find index of an element in array.


Question: write a program to find index of an element in array ?

Explanation:

A[10] = {10,20,30,40,50,60},ele = 60.

Answer: 5

Because element 60 is present at index 5.


Program:


#include<iostream>

using namespace std;

int main()

{

int A[10] = {10,20,30,40,50,60},pos,ele,flag=0;

cout<<"Enter an element to search:"<<endl;

cin>>ele;

for(int i=0;i<6;i++)

{

if(A[i] == ele)

{

pos = i;

flag = 1;

break;

}

}

if(flag==1)

{

cout<<"Element found"<<pos;

}else{

cout<<"Element not found";

}

return 0;

}

Share this

0 Comment to "FInd index of an element in array."

Post a Comment

If you have any doubts then let me know.