Prime Factors of a Number using Recursion

Spread the love

Problem Statement

Given an integer n, print all the prime factors of n.

Code

// Prime factors of a number using Recursion 

#include <stdio.h>
void prime_fact(int n)
{
    if (n==1)
      return;
int num=2;
    while (n%num != 0)
        num++;

printf("%d\n",num);
    prime_fact(n/num);
}
int main()
{
   int n;
   printf("Enter the number: ");
   scanf("%d",&n);
   prime_fact(n);
/* Tech - https://tech.arclasses.net */ 
   return 0;
}

Sample Input

24

Sample Output

2 
2 
2 
3

Read More…


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *