# Prime factors of a number in c

In this post, you will learn C program to print prime factors of a number. Before starting the coding part, it should be clear about the prime number and prime factor.

A prime number is a whole number greater than 1, whose only factors are 1 and itself. The prime numbers which divide the given number perfectly are known as the prime factors of that number. Basically, it is finding that prime numbers which multiply together to make the original number. For example, if the input number is 110, then the output should be "2 5 11". And if the input number is 99, then the output should be "3 3 11".

With the help of these two conditions, we can check whether a number is a prime factor of a given number or not -

- The number must be a prime number.
- The number must perfectly divide the given number.

It should also be clear that where we should use the prime factorization in current web technology. The prime factorization is useful when working with big numbers, such as in Cryptography, or it is commonly used in mathematical problems to secure public-key encryption systems.

## Program to find prime factors in C

Here is the program to find prime factors of a number using C. This program allows the user to enter any positive integer and find the prime factors of that -

```
// Prime factor program in C
#include<stdio.h>
#include<math.h>
void primeFactors(int num) {
int i;
// Print the number of 2s
// that divide num
while(num % 2 == 0) {
printf("%d, ", 2);
num = num/2;
}
// num must be odd at this point
for(i = 3; i <= sqrt(num); i=i+2){
// while i divides num,
// print i and divide num
while(num % i == 0) {
printf("%d, ", i);
num = num/i;
}
}
// Handle the case when num is prime
// number greater than 2
if(num > 2) {
printf("%d, ", num);
}
}
main() {
int num;
printf("Please enter any positive number to find factors : ");
scanf("%d", &num);
primeFactors(num);
}
```

**Output of the above code -**

```
Please enter any positive number to find factors : 110
2, 5, 11,
Please enter any positive number to find factors : 99
3, 3, 11,
```

In the above example, we have used the for loop and while to find the prime factors. Firstly, we have taken the input from the user and stored it in a variable num. Then, we have applied the while loop, if num is divisible by 2 then print 2 and divide num by 2. After this step, we have applied the for loop from i = 3 to the square root of num. While i divides num, print i and divide num by i. After i fails to divide num, increment i by 2 and continue. Next we print the num value, if it is greater than 2. Hence, the prime factors are printed with the single-single value of them.

### Related Articles

**Armstrong number program in c**

Write a program to check leap year in c

C program to find area of rectangle

C program to convert celsius to fahrenheit

Fibonacci series program in C using recursion

Write a program to find area of circle in C

C program to find greatest of three numbers

C program for addition of two numbers

C program to calculate compound interest

C program to find the ASCII value of a character

C program to convert Decimal to Octal

C program to convert decimal to binary

Write a C program to calculate Simple Interest

C program to check whether a number is even or odd

Write a program to check leap year in c

C program to find area of rectangle

C program to convert celsius to fahrenheit

Fibonacci series program in C using recursion

Write a program to find area of circle in C

C program to find greatest of three numbers

C program for addition of two numbers

C program to calculate compound interest

C program to find the ASCII value of a character

C program to convert Decimal to Octal

C program to convert decimal to binary

Write a C program to calculate Simple Interest

C program to check whether a number is even or odd