Home »
C programs »
C sum of series programs
C program to find the sum of the Harmonic Progression (H.P.) series
Here, we are going to learn how to find the sum of the Harmonic Progression (H.P.) series using C program?
Submitted by Nidhi, on August 01, 2021
Harmonic Progression (HP)
A series of numbers is called a harmonic progression (HP) if the reciprocal of the terms are in AP. In simple terms, a, b, c, d, e, f are in HP if 1/a, 1/b, 1/c, 1/d, 1/e, 1/f are in AP.
For two terms 'a' and 'b':
Harmonic Mean = (2 a b) / (a + b)
For two numbers, if A, G, and H are respectively the arithmetic, geometric and harmonic means, then
A ≥ G ≥ H
A H = G2, i.e., A, G, H are in GP
In the below program, we will read the total number of terms from the user and print the H.P. Series and its sum on the console screen.
C program to find the sum of the Harmonic Progression (H.P.) series
The source code to find the sum of the Harmonic Progression (H.P.) series is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to find the
// sum of Harmonic Progression (H.P.) series
#include <stdio.h>
int main()
{
int n = 0;
float i = 0;
float sum = 0;
float term = 0;
printf("Enter total term: ");
scanf("%d", &n);
sum = 0;
printf("1 ");
for (i = 1; i <= n; i++) {
printf(" + 1/%d", (int)i + 1);
term = 1 / i;
sum = sum + term;
}
printf("\nSum of H.P Series: %f\n", sum);
return 0;
}
Output
RUN 1:
Enter total term: 5
1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6
Sum of H.P Series: 2.283334
RUN2:
Enter total term: 10
1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10 + 1/11
Sum of H.P Series: 2.928968
RUN 3:
Enter total term: 3
1 + 1/2 + 1/3 + 1/4
Sum of H.P Series: 1.833333
Explanation
Here, we read the total number of terms from the user. Then we generated the H.P. series and find its total and printed them on the console screen.
C Sum of Series Programs »