C Program to Find Sum of Series 1 + 1/2 + 1/3 + 1/4 + ……. + 1/N

This is a C Program to find the sum of series 1 + 1/2 + 1/3 + 1/4 + … + 1/N.

Problem Description

This C Program calculates the Sum of Series 1 + 1/2 + 1/3 + 1/4 + … + 1/N.

Problem Solution

This program is used to find the sum of the given series.

Program/Source Code

Here is source code of the C Program to Find the Sum of Series 1 + 1/2 + 1/3 + 1/4 + … + 1/N. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to find the Sum of Series 1 + 1/2 + 1/3 + 1/4 + ... + 1/N
 */
#include <stdio.h>
 
void main()
{
    double number, sum = 0, i;
 
    printf("\n enter the number ");
    scanf("%lf", &number);
    for (i = 1; i <= number; i++)
    {
        sum = sum + (1 / i);
        if (i == 1)
            printf("\n 1 +");
        else if (i == number)
            printf(" (1 / %lf)", i);
        else
            printf(" (1 / %lf) + ", i);
    }
    printf("\n The sum of the given series is %.2lf", sum);
}
Program Explanation

In this C Program, we are reading the limit to compute the summation from the series 1/1 + 2/2 + 3/3 + ……1/N using ‘number’ integer variable.

advertisement
advertisement

For loop is used to compute the summation of each integer value. Initialize the value of ‘i’ variable to 1. Check the condition that the value of ‘i’ variable is less than or equal to the value of ‘number’ variable. If the condition is true, then execute the iteration of the loop and add the sum of series.

Nested if else condition statement is used to check the value of ‘i’ variable is equal to 1. If the condition is true then execute the statement. Otherwise, if the condition is false, execute the else if statement. Check the condition that the value of ‘i’ variable is equal to the value of ‘number’ variable.

If the condition is true then execute the statement and compute the sum of series. Otherwise, if the condition is false, then execute else statement. Print the sum of series using printf statement.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
 
Output:
$ cc pgm.c
$ a.out
 
enter the number 4
 
1 + (1/2.000000) +  (1/3.000000) +  (1/4.000000)
The sum of the given series is 2.08

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.