C Online Compiler
Example: Rhombus Side Length from User Input Diagonals in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Rhombus Side Length from User Input Diagonals #include <stdio.h> #include <math.h> // Required for sqrt() function int main() { // Step 1: Declare variables for diagonal lengths and side length double diagonal1, diagonal2, side; // Step 2: Prompt user for the first diagonal length printf("Enter the length of the first diagonal (d1): "); scanf("%lf", &diagonal1); // Step 3: Prompt user for the second diagonal length printf("Enter the length of the second diagonal (d2): "); scanf("%lf", &diagonal2); // Step 4: Validate inputs (diagonals must be positive) if (diagonal1 <= 0 || diagonal2 <= 0) { printf("Error: Diagonal lengths must be positive values.\n"); return 1; // Indicate an error } // Step 5: Calculate half the lengths of the diagonals double halfDiagonal1 = diagonal1 / 2.0; double halfDiagonal2 = diagonal2 / 2.0; // Step 6: Apply the Pythagorean theorem double sideSquared = pow(halfDiagonal1, 2) + pow(halfDiagonal2, 2); // Step 7: Calculate the side length by taking the square root side = sqrt(sideSquared); // Step 8: Print the result printf("\nRhombus Diagonal 1: %.2f units\n", diagonal1); printf("Rhombus Diagonal 2: %.2f units\n", diagonal2); printf("Calculated Rhombus Side: %.2f units\n", side); return 0; // Indicate successful execution }
Output
Clear
ADVERTISEMENTS