This blog is under construction

Saturday 22 June 2013

C program to evaluate the series 1 + 1/3 + 1/5 + ...+1/x

C program to evaluate the series 1 + 1/3 + 1/5 + ...



  #include <stdio.h>
  int main () {
        float sum = 0;
        int n, i;
        printf("Enter the value for n:");
        scanf("%d", &n);

        /* calculate the series 1+ 1/3 + 1/5 + .. */
        for (i = 1; i <= n; i = i + 2) {
                sum = sum + (1.0 / i);
        }

        printf("Output: %.3f\n", sum);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value for n:10
  Output: 1.787



No comments:

Post a Comment