C Program To Find Prime Factors of a Number using Recursion

Using this article, You are going to know about “C Program To Find Prime Factors of a Number using Recursion“. Yes, Shall we start the article to learn about it? Come on! Let’s go.

C Program To Find Prime Factors of a Number using Recursion

C Program To Find Prime Factors of a Number using Recursion

Program Code

#include<stdio.h>  
  
void pfactors_rec(int, int);  
void pfactors(int);  
  
int main()  
{  
    int num;  
  
    printf("Enter a positive integer numbern");  
    scanf("%d", &num);  
  
    printf("nPrime Factors of %d without using recursionn", num);  
    pfactors(num);  
  
    printf("nPrime Factors of %d using recursionn", num);  
    pfactors_rec(num, 2);  
  
    return 0;  
}  
  
void pfactors_rec(int num, int count)  
{  
    if(num < 1)  
        return;  
    else if(num % count == 0)  
    {  
      printf("%dn", count);  
      pfactors_rec(num / count, count);  
    }  
    else  
    {  
      pfactors_rec(num, count+1);  
    }  
}  
  
void pfactors(int num)  
{  
    int count;  
  
    for(count = 2; (num > 1); count++)  
    {  
        while(num % count == 0)  
        {  
            printf("%dn", count);  
            num = num / count;  
        }  
    }  
    printf("n");  
}

Final Words

I hope this article helps you to learn “C Program To Find Prime Factors of a Number using Recursion“. If you face any issues please let me know via the comment section. Share this article with other C/C++ program developers via social networks. Thank You!

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment