How to use the PI constant in C++?


Here we will see how to use the PI constant in C++ program. The PI constant is present in the cmath header file. The name of the constant is M_PI. We can simply include that header file, and use the constant to perform operation.

In the following example we will see how to find area of a circle using PI constant.

Example Code

#include <iostream>
#include <cmath>
using namespace std;
float area(int radius) {
   return M_PI * (radius * radius);
}
int main () {
   cout << "Area of a circle with radius 7 unit is: " << area(7);
}

Output

Area of a circle with radius 7 unit is: 153.938

Updated on: 30-Jul-2019

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements