# How to Calculate Pi in C Program3 min read

“Pi” is a mathematical constant that represents the ratio of a circle’s circumference to its diameter. It is approximately equal to 3.14159. It is often represented by the symbol “π”. The value of π can be calculated to many decimal places using various algorithms, such as the Monte Carlo method or the Chudnovsky algorithm.

In the C programming language, there are a few different ways you could go about calculating the value of π. One common method is to use the Taylor series expansion of the arctangent function. The C standard library also provides the “math.h” header, which has a few mathematical functions that include pi as a constant.

Here is an example of how you could use the Taylor series expansion to calculate π in C:

This program uses a for loop to calculate the first 100 terms of the Taylor series for the arctangent of 1, which is equal to π/4. It then prints the value of pi which is 4 times the final value of the variable term.

You could also directly use the constant pi from the math.h like this:

This program directly import math.h, where the constant pi already defined, and print its value.

Keep in mind that the value of pi from the math library is defined as a double precision floating-point value and it’s an approximation, which might not give you the most accurate value for pi.

The first code you provided is an example of how to calculate the value of π using the Taylor series expansion of the arctangent function. The Taylor series is an infinite sum that can be used to approximate a function in the vicinity of a particular point. In this case, the function we’re approximating is the arctangent of 1, which is equal to π/4.

Here’s an overview of the main parts of the code and what they do:

• The `#include <stdio.h>` at the top is a preprocessor directive that includes the standard input/output library, which allows the program to use the `printf` function to print output to the console.
• The `int main()` function is the main entry point of the program. It’s where the program’s execution starts and where the code for calculating π is located.
• The variable `i` is an iterator variable for the for loop that is used to calculate the terms of the Taylor series. The loop starts with `i = 0` and continues for 100 iterations.
• The variable `pi` is used to store the running total of the terms of the Taylor series. It is initialized to 0.0, and each term of the series is added to or subtracted from it depending on whether the term is positive or negative.
• The variable `n` is used to keep track of the denominator of each term in the series. It is initialized to 1.0, and it is incremented by 2.0 each time the loop iterates.
• The variable `term` is used to store the value of each term of the series. It is initialized to 4.0 / n, which gives the value of each term.
• Inside the for loop, there is an if-else statement checks the remainder of i/2, when i is even, the term will be added to the running total, when it’s odd, it will be subtracted.
• At the end of the loop, `pi` contains the approximate value of π, and the `printf("%f\n", pi)` statement is used to print it to the console.

The second code is much simpler where it directly includes the math.h library and assign the constant pi to a variable called pi, after that it prints the value of pi using printf.

It’s worth noting that while this code will give you a rough approximation of the value of π, it’s not the most accurate way to calculate it. There are more efficient algorithms, such as the Monte Carlo method or the Chudnovsky algorithm, that can calculate the value of π to many decimal places.