pi

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    double pi = 3.14159;

    cout << "The value of pi with default precision: " << pi << endl;

    cout << "The value of pi with precision set to 3 decimal places: ";
    cout << fixed << setprecision(3) << pi << endl;

    cout << "The value of pi with precision set to 7 decimal places: ";
    cout << fixed << setprecision(7) << pi << endl;

    return 0;
}

Explanation:

  1. #include <iostream>: Includes the input/output stream header file, allowing input and output operations.
  2. #include <iomanip>: Includes the input/output manipulator header file, which provides facilities to format output.
  3. using namespace std;: Allows the use of names for objects and variables from the standard C++ library.
  4. int main() { /* ... }: The main function where the program execution begins.
  5. double pi = 3.14159;: Declares and initializes a variable pi of type double with the value of pi (approximately 3.14159).
  6. cout << "The value of pi with default precision: " << pi << endl;: Outputs the value of pi with default precision (default number of decimal places).
  7. cout << "The value of pi with precision set to 3 decimal places: ";: Outputs a message indicating the precision change for pi.
  8. cout << fixed << setprecision(3) << pi << endl;: Sets the precision to 3 decimal places and outputs the value of pi.
  9. cout << "The value of pi with precision set to 7 decimal places: ";: Outputs a message indicating a change in precision for pi.
  10. cout << fixed << setprecision(7) << pi << endl;: Sets the precision to 7 decimal places and outputs the value of pi.
  11. return 0;: Indicates successful termination of the main() function. ```