How to use the PI constant in C++?

C++Server Side ProgrammingProgramming

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
raja
Published on 09-Apr-2019 12:26:59
Advertisements