Saturday 21 June 2014

C Program to Determine the Sum of Harmonic Series - Source Code

So, as you may know harmonic series implies:


In this program, we take the integer value of n from the user and display the sum of the series according to this value. I just used a for loop, although there are many other ways to do the same.

Here is the code:
#include <stdio.h>
#include <conio.h>

int main()
{
     int n,i;
     float inverse;
     double sum=0.0;
     //Preliminary
     printf(\"Program to find:\\n\");
     printf(\"\\n   1    1    1    1          1\\n\");
     printf(\"  __ + __ + __ + __ + ... + __\\n\");
     printf(\"\\n   1    2    3    4          n\\n\");

     //Prompting for the value for 'n'

     printf(\"\\n\\nEnter the value of n: \");
     scanf(\"%d\", &n);

     //Iterating and summing

     for(i=1; i<=n; i++)
     {
         inverse = (1.0/i);
         sum += inverse;
     }
 
     //Displaying the answer

     printf(\"\\nRequired sum is %f\", sum);
 
     getch();
     return 0; 
}

Here is the output:

Happy Coding!
:)

No comments:

Post a Comment

You might also like