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 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;
}
 
                        

🖥 Program Output

 Enter the Number: 15

 Prime factors of 15 is....
3 5
 
                            

📘 Explanation

None
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.