Source Code:

  1. //C program to calculate sum of series.
  2. //1 + (1 / 2) +  (1 / 3) + ...+ (1 / n) 
  3. #include<stdio.h>
  4. #include<conio.h>
  5. int main(){ //Variable Declaration.
  6.     float number, sum = 0, i;
  7.   //Take input from User.
  8.     printf("\n Enter the number: ");
  9.     scanf("%f", &number);
  10. //Count upto n number using loop.
  11.     for (i = 1; i <= number; i++)
  12.     {
  13.         sum = sum + (1 / i);
  14.         if (i == 1)
  15.             printf("\n 1 +");
  16.         else if (i == number)
  17.             printf(" (1 / %f)", i);
  18.         else
  19.             printf(" (1 / %f) + ", i);
  20.     }
  21.     printf("\n The sum of the given series is %f", sum); //print the output.
  22. }


Output:


 Enter the number: 5

 1 + (1 / 2.000000) +  (1 / 3.000000) +  (1 / 4.000000) +  (1 / 5.000000)
 The sum of the given series is 2.283334

0 comments:

Post a Comment