fork download
  1. #include <stdio.h>
  2. int w=1;
  3. int f(int p);
  4. int main(void) {
  5. int n;
  6. scanf("%d", &n);
  7. printf("hailstone numbers:%d, ", n);
  8. f(n);
  9.  
  10. return 0;
  11. }
  12. int f(int p)
  13. {
  14.  
  15. if(p==1)
  16. {
  17. printf("no. of steps:%d", w);
  18. }
  19. else
  20. if(p%2==1)
  21. {
  22. p=3*p+1;
  23. printf("%d, ", p);
  24. w=w+1;
  25. f(p);
  26. }
  27. else
  28. if(p%2==0)
  29. {
  30. p=p/2;
  31. printf("%d, ", p);
  32. w=w+1;
  33. f(p);
  34. }
  35. }
  36.  
Success #stdin #stdout 0s 5468KB
stdin
100
stdout
hailstone numbers:100, 50, 25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, no. of steps:26