C Program to Find all Prime Factors of the Number and Print it on Command Line

You are currently viewing C Program to Find all Prime Factors of the Number and Print it on Command Line

If you are interested to learn the topic of “C Program to Find all Prime Factors of the Number and Print it on Command Line” then you are in the perfect article. Yes, Let’s move on to this article to learn about it clearly.

C Program to Find all Prime Factors of the Number and Print it on Command Line

Source Code

/**
 * C program to find all prime factors of a given number
 */

#include <stdio.h>

int main()
{
    int i, j, num, isPrime;

    /* Input a number from user */
    printf("Enter any number to print Prime factors: ");
    scanf("%d", &num);

    printf("All Prime Factors of %d are: n", num);

    /* Find all Prime factors */
    for(i=2; i<=num; i++)
    {
        /* Check 'i' for factor of num */
        if(num%i==0)
        {
            /* Check 'i' for Prime */
            isPrime = 1;
            for(j=2; j<=i/2; j++)
            {
                if(i%j==0)
                {
                    isPrime = 0;
                    break;
                }
            }

            /* If 'i' is Prime number and factor of num */
            if(isPrime==1)
            {
                printf("%d, ", i);
            }
        }
    }

    return 0;
}

Final Thoughts

I hope this article helps you to know about “C Program to Find all Prime Factors of the Number and Print it on Command Line“. If you face any issues please let me know via the comment section. Share this article with other C/C++ developers via social networks.

Ranjith

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

Leave a Reply