Monday, August 4, 2014

Prime factors of a number

Write a program in C language to calculate the prime factors of a number.

SOLUTION:

 /*
 * Prime factors of a number 
 * Written by Shaeed Khan. 
 * Date: 04 Aug 2014
 * Version- 1.0 
 * Language: C
 */

#include < stdio.h >  
#include < stdlib.h > 

void main()
{
 int x,n;
 printf("Enter a number :");
 scanf("%d",&n);
 prime_factors(n);
}

int prime_factors(int n)
{
 int i=1,k;
 while(i<=n)
 {
  if(n%i==0)
  {
   k=check_prime(i);
   if(k!=0)
    printf("%d ",k);
  }
  i++;
 }
}

int check_prime(int n)
{
 int i=1;
 int c=0;
 while(i<=n)
 {
  if(n%i==0)
   c++;
  i++;
 }
 if(c==2)
  return n;
 else
  return 0;
}

No comments:

Post a Comment