It’s been a few years since I’ve had π day here on the C blog. Because I schedule posts for Saturdays, only thrice has a post fallen on π day: In 2015, 2020, and now in 2026.
Today is known as π day because it’s 3/14, which are the first three digits of π: 3.14159… So why not code yet another program that uses some clever mathematical trick to generate the value of π?
The secret, of course, is to locate some famous mathematician’s efforts to use a strange calculation that generates the value of π. In 2020, I used the Wallis Product. This time, I’m thieving the Leibniz Formula. Here is the mumbo jumbo artfully depicted in Figure 1:

Figure 1. The Leibniz formula for π.
Do you see a pattern? The denominators in the fractions show odd numbers: 3, 5, 7, 9, and up. The signs alternates between plus and minus. The trailing three dots denotes an infinite series.
My feeble math brain picked up on a few things right away.
First, the value 1 can be re-written as 1/1, shown in Figure 2.

Figure 1. Re-writing the value 1 as 1/1 in the Leibniz formula for π.
Second, because the result is written as π/4, it’s possible to multiple both sides of the equation by four. The result is shown in Figure 3:

Figure 3. After multiplying both sides by 4, the equation calculates the full value of π and is easier to code in C.
To work through this series you must set a denominator value starting with one and then increasing by two each operation, resulting in the series of odd numbers at the bottom of the fraction. The top value (the numerator) is always going to be four.
Further, the sign must be flipped between each operation, first subtracting the next value and then adding it. I covered this operation in an Exercise blog post from 2024.
My attempt to use the Leibniz formula to calculate the value of π, is shown in this code:
2026_03_14-Lesson.c
#include <locale.h>
#include <wchar.h>
#define PI_CHAR 0x03c0
#define ACCURACY 500000
int main()
{
float pi = 0.0;
int loop,denominator,sign;
/* use Leibniz formula to calculate pi */
denominator = 1;
sign = 0;
for( loop=0; loop<ACCURACY; loop++ )
{
pi += (sign%2?-1:1) * 4.0/denominator;
denominator+=2;
sign++;
}
/* output wide character 'pi' */
setlocale(LC_ALL,"");
wprintf(L"%lc = %.5f\n",PI_CHAR,pi);
/* clean-up */
return 0;
}
This program outputs wide characters (Unicode), so the stdio.h header file isn’t needed. Instead, the headers required for wide characters are used.
The two defined constants are the π Unicode character, PI_CHAR, and ACCURACY, which sets the number of times the main loop repeats. The greater the value of ACCURACY, the more accurate the result, though 500,000 is good enough for this program. (And it’s good enough for plotting the course of Jupiter’s orbit to within a few meters.)
The code uses a for loop to repeat ACCURACY times. Each time, the value of variable pi is increased by the value of sign (which determines whether to add or subject the next value) multiplied by 4.0 divided by the value of denomniator:
pi += (sign%2?-1:1) * 4.0/denominator;
The expression 4.0/denomniator need not be typecast to a float, at least not when using the clang compiler.
The value of denominator is increased by two: denominator+=2;, and sign is incremented: sign++;
The loop repeats, honing the value of pi with each iteration. When it’s done, the value of variable pi is output:
π = 3.14159
Though I may never understand the math, I do enjoy coding these π exercises — so much so, that I do another one next week, even though it’s not π day.
I implemented this a while ago and with 400000 iterations I got 3.14159(01)535897932(59).
The digits between () are wrong.
I handled the alternating +- a bit differently. It has to start positive as it’s flipped before each use.
long double multiplier = 1.0L;
long double denominator = 1.0L;
.
.
.
pi += ( (4.0L / (denominator += 2.0L)) * (multiplier *= -1.0L) );
When is the next Saturday?
Well, it’s Saturday again next week. But the next Saturday March 14 happens in 2037. I don’t know if I’ll still be doing this then….
I’ve just checked my diary and you’re right, there’s another Saturday this time next week. What an amazing coincidence!
Maybe you could write a program to find every pi Saturday into the far distant future. You might also like to look at the work of the Chudnovsky brothers.
On it!