Print prime factors of a given integer in decreasing order using Stack | Java 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
import java . util . * ; class GFG { static void PrimeFactors ( int N ) { Stack < Integer > st = new Stack < > ( ) ; int i = 2 ; while ( N != 1 ) { if ( N % i == 0 ) { st . push ( i ) ; while ( N % i == 0 ) { N = N / i ; } } i ++ ; } while ( ! st . isEmpty ( ) ) { System . out . println ( st . peek ( ) ) ; st . pop ( ) ; } } public static void main ( String [ ] args ) { int N = 8 ; PrimeFactors ( N ) ; ; } }