C Examples

WAP in C to use the suitable function to obtain the prime factors recursively

The Program in C to use the suitable function to obtain the prime factors recursively is given below:

#include <stdio.h>

void primeFactors(int n) {
    int i;
    if (n % 2 == 0) {
        printf("%d ", 2);
        primeFactors(n / 2);
    }
    for (i = 3; i <= sqrt(n); i += 2) {
        while (n % i == 0) {
            printf("%d ", i);
            n = n / i;
        }
    }
    if (n > 2)
        printf("%d ", n);
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    primeFactors(n);
    return 0;
}

Output:

Enter a number: 60
2 2 3 5 3 10 3 20

Pro-Tips💡

The program defines a function primeFactors which takes in one parameter, the number ‘n’ for which the prime factors need to be found.

The function uses recursion to find the prime factors of the number. It first checks if the number is divisible by 2,

if yes it prints 2 and recursively calls the function with n/2 as the new number and continues the process until the number is no longer divisible by 2.

Then it uses a for loop to iterate through the numbers starting from 3 till the square root of the number and using while loop it checks if the number is divisible by the current number

and if yes it prints that number and continues the process with n/i as the new number until the number is not divisible by the current number.

If the number is greater than 2 and not divisible by any of the numbers, it prints the number as it’s prime factor.

In the main function, it prompts the user to enter the number and calls the primeFactors function with the entered number as an argument and prints the prime factors.

here’s an example of how the program would work:

Enter a number: 60

Output:

2 2 3 5 

Explanation: The prime factors of 60 are 2, 2, 3 and 5.

The program starts with dividing the number by 2, if possible, and then continues to divide the number by the next prime numbers until the number is no longer divisible

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X