fork download
  1. #include <stdio.h>
  2. // prototype
  3. int siguma_for(int n);
  4. int siguma_while(int n);
  5. int siguma_do(int n);
  6. int siguma_rec(int n);
  7.  
  8. int siguma_for(int n)
  9. {
  10. int r = 0;
  11. for (int i = 1; i <= n; ++i)
  12. r += i;
  13. return r;
  14. }
  15.  
  16. int siguma_while(int n)
  17. {
  18. int r = 0;
  19. while (n)
  20. r += n--;
  21. return r;
  22. }
  23.  
  24. int siguma_do(int n)
  25. {
  26. int r = 0;
  27. do r += n;
  28. while (--n);
  29. return r;
  30. }
  31.  
  32. int siguma_rec(int n)
  33. {
  34. return n ? n + siguma_rec(n - 1) : 0;
  35. }
  36.  
  37. int main(void)
  38. {
  39. int n = 10;
  40. printf("%d %d %d %d\n",
  41. siguma_for(n),
  42. siguma_while(n),
  43. siguma_do(n),
  44. siguma_rec(n) );
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
55 55 55 55