...

/

Solution Review: Recursive Prime Factors

Solution Review: Recursive Prime Factors

Follow the step-by-step instructions to recursive prime factors.

We'll cover the following...

Solution

See the code given below!

Press + to interact
#include <stdio.h>
void factorize ( int n ) ;
void factorizeHelper ( int n, int i ) ;
int main( )
{
int num ;
num = 1;
printf ( "Prime factors are: " ) ;
factorize ( num ) ;
return 0 ;
}
void factorize (int n){
if (n == 1){
printf ( "%d ", n) ;
}
else{
factorizeHelper (n , 2);
}
}
void factorizeHelper ( int n, int i )
{
if ( i <= n )
{
if ( n % i == 0 )
{
printf ( "%d ", i ) ;
n = n / i ;
}
else
i++ ;
factorizeHelper ( n, i ) ;
}
}

Explanation

The recursive function, factorize( ), is called from main() to obtain the ...