Skip to main content

Series (2*3/5)+(4*5/7)+… with functions

Short answer

The i-th term is (2i)*(2i+1)/(2i+3) computed in float. sumSeries loops i=1 to n, adding term(i). For n=3, the sum is 8.724.

Write code in C to calculate the sum of following series up to n terms specified by the user where n is passed to the function that calculates the sum. Your program should have more than two functions. (23/5) + (45/7) + (6*7/9) + ........

Analysis. Looking at the series (2·3)/5 + (4·5)/7 + (6·7)/9 + …, the i-th term is

term(i) = (2i)(2i + 1) / (2i + 3)

Check: i = 1 gives 2·3/5, i = 2 gives 4·5/7, i = 3 gives 6·7/9. The sum is term(1) + … + term(n).

The program uses three functions: main() reads n, sumSeries(n) receives n and adds the terms, and term(i) returns one term. The terms are computed in float so that the division is not truncated as integer division.

#include <stdio.h>

/* i-th term = (2i)(2i+1)/(2i+3) */
float term(int i)
{
    return (float)(2 * i) * (2 * i + 1) / (2 * i + 3);
}

float sumSeries(int n)
{
    float s = 0;
    for (int i = 1; i <= n; i++)
        s += term(i);
    return s;
}

int main(void)
{
    int n;
    printf("Enter number of terms: ");
    scanf("%d", &n);
    printf("Sum of %d terms = %.3f\n", n, sumSeries(n));
    return 0;
}

Sample run (n = 3)

Input: 3

Enter number of terms: Sum of 3 terms = 8.724

Verification: 1.2 + 2.857 + 4.667 = 8.724.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 6 questions →