Print prime factors of a given integer in decreasing order using Stack | C ++ program for the above approach ; Function to print prime factors of N in decreasing order ; Stores prime factors of N in decreasing order ; Insert i into stack ; Update N ; Update i ; Print value of stack st ; Driver Code ; function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PrimeFactors ( int N ) { stack < int > st ; int i = 2 ; while ( N != 1 ) { if ( N % i == 0 ) { st . push ( i ) ; while ( N % i == 0 ) { N = N / i ; } } i ++ ; } while ( ! st . empty ( ) ) { printf ( " % d ▁ " , st . top ( ) ) ; st . pop ( ) ; } } int main ( ) { int N = 8 ; PrimeFactors ( N ) ; return 0 ; }