C++ Online Compiler
Example: Vault Height Calculator in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Vault Height Calculator #include <iostream> #include <cmath> // Required for sqrt() #include <iomanip> // Required for setprecision() and fixed using namespace std; int main() { // Step 1: Declare variables for radius, width, half-width, and height double radius; double width; double halfWidth; double height; // Step 2: Prompt user for vault radius cout << "Enter the vault's radius (e.g., 6.0 meters): "; cin >> radius; // Step 3: Prompt user for vault width (span) cout << "Enter the vault's width (e.g., 10.0 meters): "; cin >> width; // Step 4: Calculate half-width halfWidth = width / 2.0; // Step 5: Validate inputs to prevent mathematical errors and illogical geometries if (radius <= 0 || width <= 0) { cout << "Error: Radius and width must be positive values." << endl; return 1; // Indicate an error } if (halfWidth > radius) { cout << "Error: The vault's half-width cannot be greater than its radius." << endl; cout << "This would imply an impossible or inverted circular segment." << endl; return 1; // Indicate an error } // Step 6: Calculate the vault height using the formula for a circular segment // Formula: h = R - sqrt(R^2 - (w/2)^2) // Where h is height, R is radius, w is width height = radius - sqrt(pow(radius, 2) - pow(halfWidth, 2)); // Step 7: Display the calculated vault height cout << fixed << setprecision(4); // Format output to 4 decimal places cout << "\nCalculated Vault Height: " << height << " meters" << endl; return 0; }
Output
Clear
ADVERTISEMENTS