Skip to content

Pi (Leibniz series)

C alternating series

Flowchart (ISO 5807)

StartInput nπ = 0sign = 1i = 0, n − 1, 1π = π + sign·4.0 / (2i + 1)sign = −signReturn πEndFigure 1 — calcPi

Source code

C
double calcPi(int n) {
    double pi = 0;
    int sign = 1;
    for (int i = 0; i < n; i++) {
        pi += sign * 4.0 / (2 * i + 1);
        sign = -sign;
    }
    return pi;
}

The editor opens with this code — change it and the chart redraws.

Language
C
Constructs
alternating series
Topic
Loops
Formats
Word, Visio, draw.io, PDF, SVG

Related examples