Home / Programs / Write C a program to print the prime factors of a given number using a function.
Programming Example

Write C a program to print the prime factors of a given number using a function.

👁 1,150 Views
💻 Practical Program
📘 Step by Step Learning
Write C a program to print the prime factors of a given number using a function.

Information & Algorithm

If you don't know what is prime number please read form this tutorial. Prime Number & Prime Factors

Program Code

#include <stdio.h>
#include <stdbool.h>

bool isPrime(int);
int main(void)
{
	int n, d=2;
	printf("\n Enter the Number: ");
	scanf("%d",&n);
	printf("\n Prime factors of %d is....\n",n);
	for(d=2;d<=n/2;++d)
		if(n%d==0 && isPrime(d))
			printf("%d ",d);
	return 0;
}
bool isPrime(int x)
{
	int d;
	for(d=2;d<=x/2;++d)
		if(x%d==0)
			return false;
	return true;
}
 

Output

 Enter the Number: 15

 Prime factors of 15 is....
3 5
 

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.