Print the Prime Factors


/*
Write a program to print the prime factors.
*/


#include<stdio.h>

void PFactors( int num);
void IterPFactors( int n);
main()
{
 int num;
 printf("Enter a number : ");
 scanf("%d", &num);
 PFactors(num); printf("\n")
 IterPFactors(num); printf("\n");
}


void PFactors( int num)
{
 int i = 2;
 if( num == 1 )
 return;
 while( num%i != 0 )
 i++;
 printf("%d ", i);
 PFactors(num/i);
}


/*Iterative*/
void IterPFactors( int num)
{
 int i;
 for( i = 2; num!=1; i++)
 while( num%i == 0 )
 {
 printf("%d ", i);
 num = num/i;
 }
}

Post a Comment

0 Comments