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

C Programming Language / Function in C Language

1071

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

Program:

#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

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.