Skip to main content

Sum of series Σ1/n²

Short answer

Sum 1/i^2 for i=1 to N using double (1.0/(i*i)). With N=10, the result is 1.549768.

Write a program to find the sum of series Sn = Σ 1/n² up to N term.

The series is Sn = 1/1² + 1/2² + 1/3² + ... + 1/N². The program adds 1/(i*i) for i = 1 to N. A double and the real constant 1.0 are used so that the division is not an integer division (which would give 0).

#include <stdio.h>
int main(void)
{
    int n, i;
    double sum = 0.0;
    printf("Enter number of terms: ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
        sum += 1.0 / ((double)i * i);
    printf("Sn = %.6f\n", sum);
    return 0;
}

Output for N = 10:

Enter number of terms: Sn = 1.549768

(The sum tends to π²/6 = 1.6449 as N becomes large.)

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 5 questions →