Preview:
#include <iostream>
using namespace std;

void primeFactorsSieve(int n) {
  int spf[n]={0};
  
  for(int i=2;i<=n;i++) {
    spf[i]=i;
  }
  
  for(int i=2;i<=n;i++) {
    if(spf[i]==i) {
      for(int j=i*i;j<=n;j+=i) {
        if(spf[j]==j) {
          spf[j]=i;
        }
      }
    }
  }
  
  while(n!=1) {
    cout<<spf[n]<<" ";
    n=n/spf[n]; 
  }
}

int main() 
{
    int n;
    cin>>n;
    primeFactorsSieve(n);
    return 0;
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter