C++ Online Compiler
Example: Calculate Rhombus Properties from Diagonals in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Calculate Rhombus Properties from Diagonals #include <iostream> #include <cmath> // For sqrt and pow #include <iomanip> // For setprecision using namespace std; int main() { // Step 1: Declare and initialize variables for diagonals double diagonal1; double diagonal2; cout << "Enter the length of the first diagonal (d1): "; cin >> diagonal1; cout << "Enter the length of the second diagonal (d2): "; cin >> diagonal2; // Step 2: Calculate the side using the Pythagorean theorem // The diagonals of a rhombus bisect each other at right angles. // This forms four right-angled triangles. Each triangle has legs d1/2 and d2/2, // and the hypotenuse is the side 'a' of the rhombus. double side = sqrt(pow(diagonal1 / 2, 2) + pow(diagonal2 / 2, 2)); // Step 3: Calculate the area // The area of a rhombus is half the product of its diagonals. double area = (diagonal1 * diagonal2) / 2; // Step 4: Display the results cout << fixed << setprecision(2); // Format output to 2 decimal places cout << "\n--- Rhombus Properties ---" << endl; cout << "Given Diagonals: " << diagonal1 << " and " << diagonal2 << endl; cout << "Calculated Side Length: " << side << endl; cout << "Calculated Area: " << area << endl; return 0; }
Output
Clear
ADVERTISEMENTS