w3resource

C Exercises: Display sum of series 1 + 1/2 + 1/3 + ………. + 1/n


Calculate the sum of the series 1+1/2+1/3+...+1/n

Write a C program to display the sum of series 1 + 1/2 + 1/3 + ………. + 1/n.

Sample Solution:

C Code:

#include<stdio.h>
int main() {
    int num, i, sum = 0;

    // Prompt the user to input a number
    printf("Input any number: ");
    scanf("%d", &num);

    // Display the initial part of the series
    printf("1 + ");

    // Print the series
    for(i = 2; i <= num - 1; i++)
        printf(" 1/%d +", i);

    // Calculate the sum of the series
    for(i = 1; i <= num; i++)
        sum = sum + i;

    // Display the last term of the series
    printf(" 1/%d", num);

    // Calculate and display the sum
    printf("\nSum = 1/%d", sum + 1/num);

    return 0;
}

Sample Output:

Input any number: 1 +  1/0
Sum = 1/0

Flowchart:

C Programming Flowchart: Display sum of series 1 + 1/2 + 1/3 + ………. + 1/n


For more Practice: Solve these Related Problems:

  • Write a C program to compute the harmonic series sum up to n terms with careful precision handling.
  • Write a C program to calculate the harmonic series using recursion.
  • Write a C program to compute the sum of the series while skipping terms where the denominator is prime.
  • Write a C program to calculate the series sum and then compute the average of its terms.

Go to:


PREV : Find the difference between max and min of 4 numbers.
NEXT : Enumerate and display integer values for the days of the week.

C programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.