Quick actions

cmd+k|ctrl+k

Navigation

Languages

Harmonic

Snippet info

Language

C

Visibility

public

Author

krishnakanth

Created

2023-01-19T15:37:18.903191Z

Updated

2023-01-19T15:37:18.903191Z

/*b). Write a program in C to display the n terms of harmonic series and their sum. The series is :
1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms*/
#include <stdio.h>
void main()
{
 int i,n;
 float s=0.0;
 printf("Input the number of terms : ");
 scanf("%d",&n);
  printf("\n\n");
 for(i=1;i<=n;i++)
 {
 if(i<n)
 {
 printf("1/%d + ",i);
 s+=1/(float)i;
 }
 if(i==n)
 {
 printf("1/%d ",i);
 s+=1/(float)i;
 }
 }
 printf("\nSum of Series upto %d terms : %f \n",n,s);
} 
INFO