web123456

C primer plus (6th edition) Chinese version Chapter 6 Answers

  • //Question 12
  • #define _CRT_SECURE_NO_WARNINGS
  • #include <>
  • #include <>
  • int main(void)
  • {
  • int length;
  • double sum = 0.0;
  • printf("Enter the limit length: ");
  • scanf("%d", &length);
  • while (length > 0)
  • {
  • sum = 0.0;
  • for (int i = 1; i <= length; i++)
  • sum += 1.0 / i;
  • printf("The sum for 1.0 +...+ 1.0/%d.0 is %lf\n", length, sum);
  • sum = 0.0;
  • for (int i = 0; i < length; i++)
  • {
  • if (i % 2 != 0)sum = sum - 1.0 / i;
  • else
  • sum += 1.0 / i;
  • }
  • printf("The sum for 1.0 +1.0+2.0/3.0+...+ 2.0/%d.0 is %lf\n",
  • length, sum);
  • printf("Enter the limit length: ");
  • scanf("%d", &length);
  • }
  • printf("\nDone!\n");
  • return 0;
  • }