Pages

Ads 468x60px

Sunday, July 22, 2012

C++: Prime factors of a number



#include <iostream>

using namespace std;

void main()

{
int x,y,z;

cout<<"Enter a number to get its prime factors\t";

cin>>x;

if (x<=0)
{

cout<<"Enter a positive number"<<endl;
}

else
{
cout<<"The prime factors are \t";

for(y=2; y<=x; y++)
{

if(z=x%y==0)
{
cout<<y;

cout<<",\t";

x=x/y;

y=y-1;
}
}
}
}

No comments:

Post a Comment