C Program to Print Harmonic Progression(HP) Series and Sum till N Terms

  • Write a C program to print harmonic series till Nth term.
  • Write a C program to find sum of harmonic series till Nth term.

Harmonic series is a sequence of terms formed by taking the reciprocals of an arithmetic progression.
Let a, a+d, a+2d, a+3d .... a+nd be AP till n+1 terms with a and d as first term and common difference respectively. Then corresponding Harmonic series will be
1/a, 1/(a+d), 1/(a+2d), 1/(a+3d) .... 1/(a+nd).
Nth term of AP is a + (n – 1)d
Hence, Nth term of HP is reciprocal of Nth term of AP, that is 1/(a + (n – 1)d)
where, a is first term of AP and d is the common difference.

C program to print harmonic progression series and it's sum till N terms

In this program, we first take number of terms, first term and common difference as input from user using scanf function. Then we calculate the harmonic series using above formula(by adding common difference to previous term denominator) inside a for loop. We keep on adding the current term's value to sum variable.


/*
* C program to print Harmonic progression Series and it's sum till Nth term
*/
#include <stdio.h>
#include <stdlib.h>

int main() {
    int terms, i, first, denominator, diff;
    float sum = 0.0;
    printf("Enter the number of terms in HP series\n");
    scanf("%d", &terms);

    printf("Enter denominator of first term and common difference of HP series\n");
    scanf("%d %d", &first, &diff);

    /* print the series and add all elements to sum */
    denominator = first;
    printf("HP SERIES\n");
    for(i = 0; i < terms; i++) {
        printf("1/%d ", denominator);
        sum += 1/(float)denominator;
        denominator += diff;
    }

    printf("\nSum of the HP series till %d terms is %f\n", terms, sum);

    getch();
 return 0;
}
Program Output
Enter the number of terms in HP series
5
Enter denominator of first term and common difference of HP series
2 4
HP SERIES
1/2 1/6 1/10 1/14 1/18
Sum of the HP series till 5 terms is 0.893651

Related Topics
C Program to generate geometric progression(GP) series
C Program to generate arithmetic progression(AP) series
C program to find factorial of a number using recursion
C program to convert decimal numbers to binary numbers
C program to multiply two numbers without using arithmetic operators
C program to convert temperature from celsius to fahrenheit
C program to convert binary number to decimal number system
C Program to print unique elements of an unsorted array
C star triangle pattern programs
List of all C programs