# Pi(π) in C++ with Examples

• Difficulty Level : Easy
• Last Updated : 16 Jun, 2022

In this article, we will discuss some of the mathematical function which is used to derive the value of Pi(π) in C++. Method 1: Using acos() function: Approach:

1. The value of Π is calculated using acos() function which returns a numeric value between [-Π, Π].
2. Since using acos(0.0) will return the value for Π/2. Therefore, to get the value of Π:
`double pi = 2*acos(0.0);`
1. Now the value obtained from above equation is estimated as:
`printf("%f\n", pi);`

Below is the implementation of the above approach:

## CPP

 `// C++ program for the above approach` `#include "bits/stdc++.h"``using` `namespace` `std;` `// Function that prints the``// value of pi``void` `printValueOfPi()``{` `    ``// Find value of pi using``    ``// acos() function``    ``double` `pi = 2 * ``acos``(0.0);` `    ``// Print value of pi``    ``printf``(``"%f\n"``, pi);``}` `// Driver Code``int` `main()``{``    ``// Function that prints``    ``// the value of pi``    ``printValueOfPi();``    ``return` `0;``}`

Output:

`3.141593`

Time Complexity: O(1)

Auxiliary Space: O(1)

Method 2: Using asin() function: Approach:

1. The value of Π is calculated using asin() function which returns a numeric value between [-Π, Π].
2. Since using asin(1.0) will return the value for Π/2. Therefore, to get the value of Π:
`double pi = 2*asin(1.0);`
1. Now the value obtained from above equation is estimated as:
`printf("%f\n", pi);`

Below is the implementation of the above approach:

## CPP

 `// C++ program for the above approach` `#include "bits/stdc++.h"``using` `namespace` `std;` `// Function that prints the``// value of pi``void` `printValueOfPi()``{` `    ``// Find value of pi using``    ``// asin() function``    ``double` `pi = 2 * ``asin``(1.0);` `    ``// Print value of pi``    ``printf``(``"%f\n"``, pi);``}` `// Driver Code``int` `main()``{``    ``// Function that prints``    ``// the value of pi``    ``printValueOfPi();``    ``return` `0;``}`

Output:

`3.141593`

Time Complexity: O(1)

Auxiliary Space: O(1)

Method 3: Using inbuilt constant value define in the “cmath” library: The value of Pi(π) can directly written using the constant stored in cmath library. The name of the constant is M_PI. Below is the program for printing the value of Pi:

## CPP

 `// C++ program for the above approach``#include "cmath"``#include "iostream"``using` `namespace` `std;` `// Function that prints the``// value of pi``void` `printValueOfPi()``{``    ``// Print value of pi``    ``printf``(``"%f\n"``, M_PI);``}` `// Driver Code``int` `main()``{``    ``// Function that prints``    ``// the value of pi``    ``printValueOfPi();``    ``return` `0;``}`

Output:

`3.141593`

Time Complexity: O(1)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up