Tuesday, November 26, 2019

Write a program in C to display the n terms of harmonic series and their sum. 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.

#include <stdio.h>
int main()
{
   int i,num1;
   float num2=0;
   printf("Input the number of terms in the series: \n");
   scanf("%d",&num1);
  
   for(i=1;i<=num1;i++)
   {
       if(i<num1)      
      {
          printf("1/%d + ",i);
          num2+=1/(float)i;
       }

     if(i==num1)
     {
          printf("1/%d ",i);
          num2+=1/(float)i;
     }
   }
        printf("\nSum of Series upto %d terms : %f \n",num1,num2);

  return 0;
}


OUTPUT:
Input the number of terms in the series:
5
1/1 + 1/2 + 1/3 + 1/4 + 1/5
Sum of Series upto 5 terms : 2.283334

Input the number of terms in the series:
10
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10
Sum of Series upto 10 terms : 2.928968

No comments:

Post a Comment