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:
- 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:
CPP
#include "bits/stdc++.h"
using namespace std;
void printValueOfPi()
{
double pi = 2 * acos (0.0);
printf ( "%f\n" , pi);
}
int main()
{
printValueOfPi();
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
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:
CPP
#include "bits/stdc++.h"
using namespace std;
void printValueOfPi()
{
double pi = 2 * asin (1.0);
printf ( "%f\n" , pi);
}
int main()
{
printValueOfPi();
return 0;
}
|
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
#include "cmath"
#include "iostream"
using namespace std;
void printValueOfPi()
{
printf ( "%f\n" , M_PI);
}
int main()
{
printValueOfPi();
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Jun, 2022
Like Article
Save Article