fork download
  1. #include <stdio.h>
  2.  
  3. void factor(int n, int p) {
  4. if (n < 2) return;
  5.  
  6. if (n % p == 0) {
  7. printf("%d", p);
  8. if (n / p != 1) {
  9. printf(" * ");
  10. }
  11. factor(n / p, p);
  12. } else {
  13. factor(n, p + 1);
  14. }
  15. }
  16.  
  17. int main() {
  18. int n;
  19.  
  20.  
  21. do {
  22. printf("3以上の整数を入力してください: ");
  23. scanf("%d", &n);
  24. } while (n < 3);
  25.  
  26. printf("%d の素因数分解: ", n);
  27. factor(n, 2);
  28. printf("\n");
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5324KB
stdin
56
stdout
3以上の整数を入力してください: 56 の素因数分解: 2 * 2 * 2 * 7