C# Program to Find Area, Perimeter, and Side of a Rhombus
Introduction
A rhombus is a fascinating quadrilateral with equal-length sides, often described as a "diamond" shape. It has unique geometric properties that make it a significant figure in both mathematics and real-world applications. The diagonals of a rhombus intersect at right angles and bisect each other, forming the basis for many of its formulas.
In this article, we will write a C# program to calculate the area, perimeter, and side of a rhombus using user-provided input for the lengths of its diagonals.
Formula
- Area: The area of a rhombus is calculated using the formula:
Area = (Diagonal1 × Diagonal2) / 2
. Here,Diagonal1
andDiagonal2
are the lengths of the diagonals. - Perimeter: The perimeter is the total length of all four sides:
Perimeter = 4 × Side
. - Side: Using the Pythagorean theorem:
Side = √((Diagonal1/2)² + (Diagonal2/2)²)
.
Code
using System; class Rhombus { static void Main() { // Input the lengths of the diagonals Console.Write("Enter the length of Diagonal 1: "); double diagonal1 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter the length of Diagonal 2: "); double diagonal2 = Convert.ToDouble(Console.ReadLine()); // Calculate the area double area = (diagonal1 * diagonal2) / 2; // Calculate the side length double side = Math.Sqrt(Math.Pow(diagonal1 / 2, 2) + Math.Pow(diagonal2 / 2, 2)); // Calculate the perimeter double perimeter = 4 * side; // Output the results Console.WriteLine("Area of the Rhombus: " + area); Console.WriteLine("Side of the Rhombus: " + side); Console.WriteLine("Perimeter of the Rhombus: " + perimeter); } }
Output
For example, if the lengths of the diagonals are Diagonal1 = 12
and Diagonal2 = 10
, the program will calculate and display the following results:
Enter the length of Diagonal 1: 12 Enter the length of Diagonal 2: 10 Area of the Rhombus: 60 Side of the Rhombus: 7.810249675906654 Perimeter of the Rhombus: 31.240998703626617
Explanation
The program begins by prompting the user to input the lengths of the diagonals. Using the provided formulas:
- The area is calculated by multiplying the diagonals and dividing the result by 2.
- The side is determined using the Pythagorean theorem. Each diagonal is halved to form two right triangles within the rhombus, and the hypotenuse of one triangle gives the side length.
- The perimeter is computed by multiplying the side length by 4.
The results are displayed on the console in a user-friendly format.
Conclusion
This C# program provides a straightforward way to compute the geometric properties of a rhombus based on its diagonals. By combining mathematical principles with efficient C# code, users can quickly and accurately find the area, perimeter, and side of a rhombus.