Advertisement
Guest User

prime factor

a guest
Jul 10th, 2010
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include<conio.h>
  2. #include<STDIO.H>
  3. void main(void)
  4. {
  5. int number;
  6. int prime(int number);
  7. int primefactor(int number);
  8. printf("Enter the number whose prime factors are to be calculated:");
  9. scanf ("%d", &number);
  10. primefactor(number);
  11. getch();
  12. }
  13.  
  14. //The following is the function that detects a Prime number.
  15.  
  16. int prime(int num)
  17. {
  18. int i, ifprime;
  19. for (i=2; i<=num-1; i++)
  20. {
  21. if (num%i==0)
  22. {
  23. ifprime=0;
  24. }
  25. else
  26. ifprime=1;
  27. }
  28. return
  29. (ifprime);
  30. }   //The following function prints the prime factors of a number.
  31.  
  32. int primefactor(int num)
  33. {
  34.     int factor,ifprime;
  35.     for (factor=2; factor<=num;factor++)
  36.     {
  37.  
  38.         prime(factor);
  39.         //so that the factors are only prime and nothing else.
  40.         if (ifprime)
  41.         {
  42.             if (num%factor==0) //diving by all the prime numbers less than the number itself.
  43.             {
  44.                 printf("%d ", factor);
  45.                 num=num/factor;
  46.                 factor--;
  47.                   //    continue;
  48.             }
  49.               //    else
  50.               //    {
  51.               //    factor++;//this cannot be made a part of the for loop
  52.                //   }
  53.         }
  54.         }
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement