C Program to Find Prime Factors Of A Given Number

#include<stdio.h>
/* FUNCTION PROTOTYPES */
void getPrimeFactors();
/* MAIN FUNCTION*/
int main()
{
 int number;
 printf("Enter number: ");
 scanf("%d", &number);
 /* FUNCTION CALL */
 getPrimeFactors(number);
 return 0;
}

/* FUNCTION DEFINITION */
void getPrimeFactors(int n)
{
    int i = 2, p[100], index=0;
    while(i*i <= n)
    {
        if(n%i==0)
        {
            p[index] = i;
            index++;
            n = n/i;
        }
        else
        {
            i = i+1;
        }
    }
    if(n>1)
    {
        p[index] = n;
    }
    /* Displaying results */
    printf("Prime factors are:\n");
    for(i=0;i<=index;i++)
    {
        printf("%d\t", p[i]);
    }
}

Output

Enter number: 12321
Prime factors are:
3       3       37      37

Leave a Reply

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

https://atozonlinesolutions.com/wp-content/uploads/2017/03/logo_white.png
https://atozonlinesolutions.com/wp-content/uploads/2017/03/logo_white.png
About
Products
Subscribe

If you wish to receive our latest news in your email box, just subscribe to our newsletter. We won’t spam you, we promise!

    Applauz

    As the pioneer of the lean startup movement, APPLAUZ has dedicated it’s time to sharing effective business strategies that help new businesses and enterpreneurs put their money to work in the right way.

    Subscribe

    If you wish to receive our latest news in your email box, just subscribe to our newsletter. We won’t spam you, we promise!

      Applauz

      As the pioneer of the lean startup movement, APPLAUZ has dedicated it’s time to sharing effective business strategies that help new businesses and enterpreneurs put their money to work in the right way.

      Copyright by BOLD THEMES. All rights reserved.