Prime Pactorization

Here is an awesome program to find the prime factorization of a number..


#include<iostream.h>
int main()
{
long n,i,cpy,j;
cout<<"ENTER THE NUMBER: ";
cin>>n;
cpy=n;
for(i=1;i<=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
break;
}
if(j<i||i==1)
;
else
 {
 if(cpy%i==0)
  {
cpy=cpy/i;
cout<<i;
if(cpy==1)
;
else
cout<<'*';
}
  }
}
return 0;
}

Preview..


If you have any doubt please comment..

No comments:

Post a Comment