fork download
  1. // question 1 (You have given a series, 1+2+3-4-5-6+7+8+9-10-11-12+......N . Your task to print the sum of the Nth element.)
  2.  
  3. #include <stdio.h>
  4.  
  5. void num(int a);
  6.  
  7. int main()
  8. {
  9. int y;
  10. scanf("%d", &y);
  11.  
  12. num(y);
  13. return 0;
  14. }
  15.  
  16. void num(int a)
  17. {
  18.  
  19. int i, sum1 = 0, calc = 0;
  20.  
  21. for (i = 1; i <= a; i++)
  22. {
  23.  
  24. if (calc % 2 == 0)
  25. sum1 += i;
  26. else
  27. sum1 -= i;
  28.  
  29. if (i % 3 == 0)
  30. calc++;
  31. }
  32.  
  33. printf("%d ", sum1);
  34. }
Success #stdin #stdout 0.01s 5436KB
stdin
20
stdout
12