C++ Online Compiler
Example: Calculate Missing Side from Diagonal and One Side in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Calculate Missing Side from Diagonal and One Side #include <iostream> #include <cmath> #include <iomanip> int main() { // Step 1: Declare variables double diagonal, knownSide, missingSide; // Step 2: Prompt user for diagonal length std::cout << "Enter the length of the diagonal: "; std::cin >> diagonal; // Step 3: Prompt user for the length of one known side std::cout << "Enter the length of one known side: "; std::cin >> knownSide; // Step 4: Check for valid input if (diagonal <= 0 || knownSide <= 0) { std::cout << "Error: Diagonal and side lengths must be positive." << std::endl; return 1; } if (knownSide >= diagonal) { std::cout << "Error: A side length cannot be greater than or equal to the diagonal." << std::endl; return 1; } // Step 5: Calculate the missing side // missing_side = sqrt(diagonal^2 - known_side^2) missingSide = std::sqrt(std::pow(diagonal, 2) - std::pow(knownSide, 2)); // Step 6: Display the result std::cout << std::fixed << std::setprecision(2); std::cout << "The length of the missing side is: " << missingSide << std::endl; return 0; }
Output
Clear
ADVERTISEMENTS