Programming Example
Write C a program to print the prime factors of a given number using a function.
Write C a program to print the prime factors of a given number using a function.
If you don't know what is prime number please read form this tutorial. Prime Number & Prime Factors
#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;
}
Enter the Number: 15
Prime factors of 15 is....
3 5
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.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.