Prime Factor | Program to print all prime factors ; A function to print all prime factors of a given number n ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver program to test above function
# include <stdio.h> NEW_LINE # include <math.h> NEW_LINE void primeFactors ( int n ) { while ( n % 2 == 0 ) { printf ( " % d ▁ " , 2 ) ; n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { printf ( " % d ▁ " , i ) ; n = n / i ; } } if ( n > 2 ) printf ( " % d ▁ " , n ) ; } int main ( ) { int n = 315 ; primeFactors ( n ) ; return 0 ; }