Q9 A positive integer is entered through the keyboard, write a program to obtain the prime factors of the number. Modify the function suitably to obtain the prime factors recursively.

A positive integer is entered through the keyboard, write a program to obtain the prime factors of the number. Modify the function suitably to obtain the prime factors recursively.

Program: 130

A positive integer is entered through the keyboard, write a program to obtain the prime factors of the number. Modify the function suitably to obtain the prime factors recursively.

How to use recursion to get the prime factors of a positive integer.

#include<stdio.h>
void prime(int num);
void main()
{
    int num;

    printf("Enter an integer: ");
    scanf("%d", &num);
    printf("The Prime Factors of %d are: ",num);
    prime(num);
}

void prime(int num)
{
    int x;
    for(x=2;x<=num;x++)
    {
        if(num%x==0)
        {
            printf("%d, ",x);
            prime(num/x);
            break;
        }
    }
}

Output:

Enter an integer: 12
The Prime Factors of 12 are: 2, 2, 3,

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.