How To Get The Value Of Pi In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / How To Get The Value Of Pi In C++

How To Get The Value Of Pi In C++

BLUF: Mastering How To Get The Value Of Pi In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: How To Get The Value Of Pi In C++

C++ is renowned for its efficiency. Learn how How To Get The Value Of Pi In C++ enables low-level control and high-performance computing in the tutorial below.

In this tutorial, you will discover various techniques to calculate the value of Pi in C++. Several mathematical functions within the C++ language can be employed to derive the value of Pi (π).

1. Making Use of Pre-Defined Constants

The M_PI constant is present in both C and C++ programming languages and is commonly located in the math.h or cmath header. This constant represents the mathematical value of Pi and can be directly utilized for calculations involving Pi in your C++ code.

Example:

Let's consider an example to retrieve the value of pi by utilizing predefined constants in C++:

Example

#include <iostream>
#include <cmath>
int main()
{
    double pi = M_PI;
    std::cout << "The value of Pi is: " << pi << std::endl;
    return 0;
}

Output:

Output

The value of Pi is: 3.14159

Explanation:

  • The header files required for input/output operations (<iostream>) and mathematical functions (<cmath>) are included using the #include directives.
  • The program's entrance point serves as its main function .
  • double pi = MPI; line initializes a double variable pi to the value of the predefined constant MPI, which stands for the mathematical constant Pi. Usually, the <cmath> or <math.h> header file contains this constant.
  • The words "The value of Pi is: " and the value of pi is printed to the standard output stream using the std::cout statement. After printing the message, a new line is added using the std::endl function.
  • return 0; signifies the end of the main function and the successful execution of the program.
  • 2. Making Use of Mathematical Functions

A numerical value ranging from negative to positive is generated by the acos function, which is employed in determining the numerical value of pi.

Calculating the inverse cosine of 0.0 will yield the value for π/2.

An estimation of Pi can also be achieved by employing various mathematical functions, like acos(-1) or atan(1) * 4. These formulas utilize trigonometric or inverse trigonometric functions to calculate the numerical value of Pi.

Example:

Let's consider an example to calculate the value of pi using mathematical functions in C++:

Example

#include <iostream>
#include <cmath>
int main()
{
    double pi_approx = atan(1) * 4;
    std::cout << "An approximation of Pi is: " << pi_approx << std::endl;
    return 0;
}

Output:

Output

An approximation of Pi is: 3.14159

Explanation:

  • The header files required for input/output operations (<iostream>) and mathematical functions (<cmath>) are included using the #include directives.
  • The main function serves as the program's execution's beginning point.
  • double pi_approx = atan(1) * 4; line uses the atan function to compute an approximation of Pi. The arctangent of 1, which is equal to /4 in radians, is calculated using the phrase atan(1) . An approximate value of π can be obtained by multiplying this value by 4.
  • The text "An approximation of Pi is: " and pi_approx's value is printed to the standard output stream using the std::cout command. After printing the message, a new line is added using the std::endl function.
  • return 0; signifies the end of the main function and the successful execution of the program.
  • 3. Utilizing the asin function

The asin function calculates the value of Π and provides a numerical output within the range of [-Π, Π]. When asin(1.0) is computed, it yields the result for Π/2. Therefore, in order to determine the value of Π:

Example

double pi = 2*asin(1.0);

The result obtained from the formula mentioned above is now computed in the subsequent manner:

Example

printf("%f\n", pi);

Example:

Let's consider an example to calculate the value of pi by utilizing the asin function in C++:

Example

#include "bits/stdc++.h"
using namespace std;
// Function that prints the
// value of pi
void print_ValueOfPi()
{
    // Find value of pi using
    // asin() function
    double pi = 2 * asin(1.0);
    // Print value of pi
    printf("The value of pi is : %f\n", pi);
}
int main()
{
    // Function that prints
    // the value of pi
    print_ValueOfPi();
    return 0;
}

Output:

Output

The value of pi is: 3.141593

Explanation:

  • #include directive in competitive programming environments, the most standard libraries can be included by using the shorthand "bits/stdc++.h" . It contains every header file from the standard library.
  • Program items can be used from the standard namespace without being explicitly specified due to the using namespace std; declaration .
  • It is possible to compute and print the value of Pi using the print_ValueOfPi function, which is defined.
  • The formula to find Pi is double pi = 2 * asin(1.0). When the arc sine of an argument is calculated using the asin function, it returns π/2, which may be multiplied by two to find Pi.
  • The program's execution entry point is known as the main function .
  • The main function calls the print_ValueOfPi function to print the value of Pi.
  • return 0; signifies the successful execution of the program and the end of the main function

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience