This blog is under construction

Saturday 22 June 2013

C program to evaluate the series 1 + 1/2 + 1/3 + .. + 1/N

Write a C program to evaluate the given series (S = 1 + 1/2 + 1/3 + .. + 1/N ).


  #include <stdio.h>
  int main() {
        int i, n;
        float sum = 0;

        printf("Enter the value for n:");
        scanf("%d",  &n);

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

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




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


No comments:

Post a Comment