fork download
  1. // The sum of 1+(1-2)+(1-2+3)+(1-2+3-n)... where even integers are -k and odd integers are +k.
  2. #include <stdio.h>
  3.  
  4. int ancho(int n)
  5. {
  6. int sum=0;
  7. for(int i=1; i<=n; ++i)
  8. {
  9. for(int j=1; j<=i; ++j)
  10. {
  11. sum += (2*(j%2)-1)*j;
  12. }
  13. }
  14. return sum;
  15. }
  16.  
  17. int main(void)
  18. {
  19. int n = 5;
  20.  
  21. printf("Solution is %d\n", ancho(n));
  22. }
  23.  
  24. // Solution is 0 for n = 5,
  25. // because: 1 + (1-2) + (1-2+3) + (1-2+3-4) + (1-2+3-4+5) =
  26. // 1-1+2-2+3 =
Success #stdin #stdout 0s 5476KB
stdin
Standard input is empty
stdout
Solution is 3