08 March, 2013

C++ Program To Find Number Of Times The Given Number Occurred In An Array

This Post Contains A C++ Program To Find Number Of Times The Given Number Occurred In An Array With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'If...else' & 'For-Loop' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Find Number Of Times The Given Number Occurred In An Array.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>


/* Start Of Main Program */
void main()
{

   /* Declaration Of Variables */
   int a[10], num, i, counter=0;

   clrscr();

   /* Asking For The Input From User */
   cout<<"\n Enter 10 Values :";
   for(i=0; i<10; i++)
   {
      cin>>a[i];
   }
   cout<<"\n Enter Any No. From Array For Counting :";
   cin>>num;
   for(i=0; i<10; i++)
   {
      if(num == a[i])
      {
         counter++;
      }
   }

   /* Printing The Output Onto The Screen/Console */
   if(counter == 0)
   {
      cout<<"\n"<< num <<"Does Not Exist In The Array";
   }
   else
   {
      cout<<"\n"<< num <<"Occurred"<< counter <<"Times";
   }
   getch();
}
/* End Of Main Program */

Output :

Enter 10 Values :
1  6  3  4  5  6  7  8  6  10

Enter Any No. From Array For Counting : 6

6 Occurred 3 Times




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.