Evaluates running totals of these two series up to some limit of number of terms. - C Operator

C examples for Operator:Arithmetic Operator

Introduction

Consider these two infinite series:

1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ...
1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 + ...

Demo Code

#include <stdio.h>

int main(void)
{
  long int limit;
  float sign = 1.0f;
  float series1 = 0, series2 = 0;

  printf("Enter a number of terms to sum: ");
  scanf("%ld", &limit);

  for (long int i = 1; i <= limit; i++)
  {//from w w w.ja v  a 2s.  c om
    series1 += 1.0f/i;
    series2 += (1.0f/i) * sign;
    sign = -sign; // toggle sign
  }

  printf("The %ldth partial sum for series 1 is: %.5f\n", limit, series1);
  printf("The %ldth partial sum for series 2 is: %.5f\n", limit, series2);

  printf("Series 1 has no limit. Series 2 appears to be bounded above\n");

  return 0;
}

Result


Related Tutorials