Home / Programs / C Program to print the prime factors using recursion
🚀 Programming Example

C Program to print the prime factors using recursion

👁 2,915 Views
💻 Practical Program
📘 Step Learning
Program to print the prime factors using recursion

📌 Information & Algorithm

If you don't know what is prime number please read form this tutorial. Prime Number & Prime Factors

💻 Program Code

#include<stdio.h>
void PFactors( int num);
void IPFactors( int n);
main( )
{
	int num;
	printf("Enter a number : ");
	scanf("%d", &num);
	PFactors(num); printf("\n");
	IPFactors(num); printf("\n");
}/*End of main()*/

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

}/*End of PFactors()*/

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

🖥 Program Output

Enter a number : 145
5 29
5 29
Press any key to continue . . .
                            

📘 Explanation

Program to print the prime factors using recursion
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.