Use a constant expression to represent the value of PI, calculate the area of a circle based on a radius value entered by the user. - C++ Function

C++ examples for Function:Function Return

Description

Use a constant expression to represent the value of PI, calculate the area of a circle based on a radius value entered by the user.

Demo Code

#include <iostream> 

// get an approximate value of PI 
constexpr double getPi() { 
    return (double) 22 / 7; 
} 
 
int main() { /* w w w .  j av  a 2  s.c  o  m*/
    float radius; 
 
    std::cout << "Enter the radius of the circle: "; 
    std::cin >> radius; 
 
    // the area equals PI * the radius squared 
    double area = getPi() * (radius * radius); 
 
    std::cout << "\nCircle's area: " << area << std::endl; 
 
    return 0; 
}

Result


Related Tutorials