C++ Online Compiler
Example: Calculate Missing Rhombus Diagonal and Area in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Calculate Missing Rhombus Diagonal and Area #include <iostream> #include <cmath> // For sqrt and pow #include <iomanip> // For setprecision using namespace std; int main() { // Step 1: Declare and initialize variables for side and one diagonal double side; double knownDiagonal; cout << "Enter the side length of the rhombus (a): "; cin >> side; cout << "Enter the length of one diagonal (d1): "; cin >> knownDiagonal; // Step 2: Calculate the missing diagonal // From the Pythagorean theorem: a^2 = (d1/2)^2 + (d2/2)^2 // We want to find d2, so (d2/2)^2 = a^2 - (d1/2)^2 // d2 = 2 * sqrt(a^2 - (d1/2)^2) // Check for valid input to prevent square root of a negative number if (pow(side, 2) < pow(knownDiagonal / 2, 2)) { cout << "Error: The given diagonal is too long for the given side length." << endl; cout << "A half-diagonal cannot be longer than the side." << endl; return 1; // Indicate an error } double halfMissingDiagonal = sqrt(pow(side, 2) - pow(knownDiagonal / 2, 2)); double missingDiagonal = 2 * halfMissingDiagonal; // Step 3: Calculate the area double area = (knownDiagonal * missingDiagonal) / 2; // Step 4: Display the results cout << fixed << setprecision(2); // Format output to 2 decimal places cout << "\n--- Rhombus Properties ---" << endl; cout << "Given Side Length: " << side << endl; cout << "Given Diagonal: " << knownDiagonal << endl; cout << "Calculated Missing Diagonal: " << missingDiagonal << endl; cout << "Calculated Area: " << area << endl; return 0; }
Output
Clear
ADVERTISEMENTS