Calculate the sum of the first N terms of the harmonic series:
\(1 + \frac{1}{2}+\frac{1}{3}+\cdots+\frac{1}{N}\)
Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
void main()
{
int N;
int I;
float SUM;
do
{
printf ("Number of terms: ");
scanf ("%d", &N);
}while (N<1);
for (SUM=0.0, I=1 ; I<=N ; I++)
SUM += (float)1/I;
printf("Sum of the first %d terms is equal to %f \n", N, SUM);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Static implementation of a deque