To find prime factor of a number

#include<iostream>
#include<conio.h>
#include<math.h>
#define MAX 50
using namespace std;
int main()
{
int p,a[MAX],n,l[MAX],j,i;
cout<<"Enter no. (number must be greater than 2) : ";
cin>>n;
for(p=2;p<=n;p++)
a[p]=p;
for(p=2;p<=(sqrt(n));p++)
{
if (a[p]!=0)
{
j=p*p;
while (j<=n)
{
a[j]=0;
j=j+p;
}
}
}
i=0;
for(p=2;p<=n;p++)
{
if (a[p]!=0)
{
l[i]=a[p];
i=i+1;
}
}
cout<<"Prime factor of "<<n<<" is: \n";
for(p=0;p<i;p++)
cout<<" | "<<l[p];
cout<<" |";
getch();
return 0;
}
Console Screen Output: -

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons