Back to Home Page

/*******************************************************************************
Problem: Write a program that will read in a long integer number and will
output how many times each digit is present in the number.
Example, Input/Output:

Enter a number: 723345167
Digit                   # of Repetitions
-----       &            ----------------
1                                 1
2                                 1
3                                 2
4                                 1
5                                 1
6                                 1
7                                 2

Course: C++
Tutor : Guillermo Julca
Mercy College

View Output
*******************************************************************************/

//---------------------------------------------------------------------------
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
long int number, auxn;
int digit, dig, counter;
cout<<"Enter a number : ";
cin>>number;
cout<<endl;
cout<<"Digit"<<setw(25)<<"# of Repetitions"<<endl;
cout<<"-----"<<setw(25)<<"------------------"<<endl;
for (digit = 0; digit <=9; digit++)
{
auxn= number;
counter = 0;
while ( auxn != 0)
{
dig = auxn % 10;
auxn = auxn / 10;
if (dig == digit)
counter++;
}// end while ( auxn != 0)
if (counter > 0)
cout<<setw(3)<<digit<<setw(15)<<counter<<endl;
}// end for (digit = 0; digit <=9; digit++)
getchar();
return 0;
}

View Output
//---------------------------------------------------------------------------

Back to Home Page



Copyright  © 2002                                   GJ  GUILLERMO JULCA