In this article, we will discuss some of the mathematical function which is used to derived the value of Pi(π) in C++.
Method 1: Using acos() function:
Approach:
- The value of Π is calculated using acos() function which returns a numeric value between [-Π, Π].
- Since using acos(0.0) will return the value for Π/2. Therefore to get the value of Π:
double pi = 2*acos(0.0);
- Now the value obtained from above equation is estimated as:
printf("%f\n", pi);
Below is the implementation of the above approach:
// 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; } |
3.141593
Method 2: Using asin() function:
Approach:
- The value of Π is calculated using asin() function which returns a numeric value between [-Π, Π].
- Since using asin(1.0) will return the value for Π/2. Therefore to get the value of Π:
double pi = 2*asin(1.0);
- Now the value obtained from above equation is estimated as:
printf("%f\n", pi);
Below is the implementation of the above approach:
// 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; } |
3.141593
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:
// 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; } |
3.141593
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- std::less in C++ with Examples
- cin get() in C++ with Examples
- std::is_default_constructible in C++ with Examples
- std::add_const in C++ with Examples
- std::is_copy_assignable in C++ with Examples
- std::is_trivially_constructible in C++ with Examples
- std::is_literal_type in C++ with Examples
- std::is_move_assignable C++ with Examples
- std::is_member_pointer in C++ with Examples
- std::is_trivially_destructible in C++ with Examples
- std::is_nothrow_default_constructible in C++ with Examples
- std::equal_to in C++ with Examples
- ratio_equal() in C++ with examples
- std::bit_xor in C++ with Examples
- std::is_nothrow_assignable in C++ with Examples
- std::add_cv in C++ with Examples
- std::add_volatile in C++ with Examples
- negative_binomial_distribution in C++ with Examples
- mbrtoc16() in C/C++ with Examples
- mbrtoc32() in C/C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

