Write a Program in C++ which give the Index number of a Number in Array

Array is a Data Structure that hold the data on different index. In this problem we learn how to get the index number of an array. The program will give output which is the index number of the number in array.

Source Code


#include<iostream>
using namespace std;
int main ()
{
 int num [5];
 int index=0;
 for(int i=0;i<5;i++)
 {
 cout<< " Enter Your Number :";
 cin>>num[i]; 
  }
  cout<<" Enter the Number to get it index number : ";
  cin>>index;
  for (int i =0;i<5;i++)
  {
   if(num[i]==index)
  {
   cout<<" \n \n Your Number Exist in Array Index :"<<i;
  } 
  }
}

Output of the Program


C++ Array Programming

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

3 comments:

  1. That is the most contrived way of saying: "Write a search function" ever.

    ReplyDelete
  2. I find using cout inside loops like that teaches poor structure.
    Sure, it prints out for every entry but I think it would be better to store the result and exit the loop.
    Also, indentation seems to have been lost. Probably down to the style sheet.

    ReplyDelete