Back to Home Page

/*****************************************************************************
Problem: Write a program that allow to the user calculates the prime factors
of any number with its powers (exponents).Your program should include
a loop that lets the user evaluate other number again until the
user says he or she is done.
Example; Enter a number : 17424 //input
// Output in the screen should be:

Factor    Exponent
2               4
 3               2
11              2

Do you want to continue ( Y/N) ? : _

Note: 17424 = (2^4) * (3^2) * (11^2)
Hint: use integer division ( / ) and % operator

Course: C++
Tutor : Guillermo Julca
Mercy College

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

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

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

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

#pragma argsused
int main(int argc, char* argv[])
{
int number;
int factor;
int exponent;
char ans;
do
{
cout<<"Enter a Number : ";
cin>> number;
cout<<"Factor"<<setw(15)<<"Exponent"<<endl;
factor = 2;
while ( number != 1)
{
exponent = 0;
while ( number % factor == 0)
{
exponent ++;
number = number / factor;
} // end while ( number % factor == 0)
if ( exponent > 0 )
cout<<setw(3)<<factor<<setw(15)<<exponent<<endl;
factor ++;
} // end while ( number <> 1)
cout<<"Do you want to continue (Y/N) ? : ";
cin>> ans;
} while ( ans =='Y' || ans =='y' );
getchar();
return 0;
}

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

Back to Home Page



Copyright  © 2002                                   GJ  GUILLERMO JULCA