fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int factorise(int a, int factor)
  5. {
  6.  
  7. if (a % factor == 0)
  8. {
  9. while (a % factor == 0)
  10. { a/=factor;
  11. printf("%d ", factor);
  12. }
  13. return a;
  14. }
  15. else
  16. {
  17. return a;
  18. }
  19. }
  20. int main()
  21. {
  22. int n, i;
  23. printf("Enter the number to factorise - ");
  24. scanf("%d", &n);
  25. printf("prime factors are - \n");
  26. if (n <= 0) {
  27. printf("Prime factorization is undefined for non-positive numbers.\n");
  28. return 1;
  29. }
  30. if (n == 1) {
  31. printf("No prime factors for 1.\n");
  32. return 0;
  33. }
  34. while (n % 2 == 0)
  35. {
  36. printf("2 ");
  37. n /= 2;
  38. }
  39. for (i = 3; i <= sqrt(n); i += 2)
  40. {
  41. n = factorise(n, i);
  42. }
  43.  
  44. if (n > 2)
  45. {
  46. printf("%d ", n);
  47. }
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Enter the number to factorise - prime factors are - 
3 5 31 47