Friday 24 August 2012

How to serach a number in an array and count its occurance in C++






#include<iostream>

#include<conio.h>

using namespace std;

int main()
{
      int numbers[9], count = 0, temp = 0, number = 0;

      cout << "\t\tSize of the array is limited to 9";

      cout << "\n\n  Enter  the Numbers - \n\n";

      for(temp = 0; temp < 9; temp++)
      {
            cout << "\n\t" << temp + 1 << " number - ";

            cin >> numbers[temp];
      }

      cout << "\n\n  Enter the Number to be Searched - ";
     
      cin >> number;

      for(temp = 0; temp < 9; temp++)
      {
            if (numbers[temp] == number)
            {
                  count++;
            }
      }

      if (count != 0)
      {
            cout << "\n\n\t\t __ " <<  number << " is found " << count << " times __";
      }
      else
      {
            cout << "\n\n\t\t __ " <<  number << " is not found __";
           
      }
     

      getch();

      return 0;

}

No comments:

Post a Comment