C Program To Find Prime Factors of a Number


A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number.

For Example: Prime factors of 24 are 2, 2, 2 and 3. Whereas prime factors of 35 are 5 and 7.

Related Read:
C Program to Find Factors of a Number
C Program To Find Prime Number or Not using While Loop

Factors of a Number: All the numbers which perfectly divide a given number are called as Factors of that number.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Note: The user entered number need not be a prime number. But the factors of the number must be prime number.

For example, 24 and 35 are not prime numbers. But the prime factors of 24 are 2, 2, 2, and 3. Here both 2 and 3 are prime numbers. Similarly, prime factors of 35 are 5 and 7. Both 5 and 7 are prime numbers.

Video Tutorial: C Program To Find Prime Factors of a Number using Function


[youtube https://www.youtube.com/watch?v=QAxnHPxicgY]

YouTube Link: https://www.youtube.com/watch?v=QAxnHPxicgY [Watch the Video In Full Screen.]


Source Code: C Program To Find Prime Factors of a Number using Function

#include<stdio.h>

void primefactors(int num)
{
    int count;

    printf("\nPrime Factors of %d are ..\n", num);
    for(count = 2; num > 1; count++)
    {
        while(num % count == 0)
        {
            printf("%d ", count);
            num = num / count;
        }
    }
    printf("\n");
}

int main()
{
    int num;

    printf("Enter a positive integer\n");
    scanf("%d", &num);

    primefactors(num);

    return 0;
}

Output 1:
Enter a positive integer
24

Prime Factors of 24 are ..
2 2 2 3

Output 2:
Enter a positive integer
35

Prime Factors of 35 are ..
5 7

Output 3:
Enter a positive integer
315

Prime Factors of 315 are ..
3 3 5 7

Output 4:
Enter a positive integer
24024

Prime Factors of 24024 are ..
2 2 2 3 7 11 13

Output 5:
Enter a positive integer
510

Prime Factors of 510 are ..
2 3 5 17

Logic To Find Prime Factors of a Number, using Function

We ask the user to enter a positive integer number and store it inside variable num. We pass this value to a function primefactors().

Inside primefactors() function we write a for loop. We initialize the loop counter to 2 – which is the smallest prime number. We exit the for loop when num is less than or equal to 1.

Inside for loop we write a while loop to check if the number is perfectly divisible by the value of count. If yes, then we display the value of count and divide the num by count and store it back into variable num.

Since we continuously modulo divide and divide the number by 2, other numbers(which are multiples of 2) can not divide the number. Similarly, we divide the number by 3, so other numbers(which are multiples of 3) can not divide the number. Next we divide the number by 5, 7, 11, etc.

Note: Function primefactors() doesn’t return anything so its return type is void. It accepts 1 integer type argument.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

2 thoughts on “C Program To Find Prime Factors of a Number”

Leave a Reply

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