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