Posted on by Kalkicode
Code Geometric

# Find the Area of Pentagon

The problem involves calculating the area of a regular pentagon, a five-sided polygon where all sides and angles are equal. The area of a pentagon can be determined using a formula that takes the length of a side as input. Calculating the area of a pentagon is a geometric calculation used in mathematics, architecture, and engineering.

## Problem Statement

Given the length of a side of a regular pentagon, we need to calculate and output the area of the pentagon. The side length is provided as input, and we are required to use the formula for the area of a pentagon to find the area.

## Example

Consider a regular pentagon with a side length of 6 units. This pentagon can be visualized as follows:

``````
/\
/  \
/    \
/      \
/________\
6.0``````

## Idea to Solve

To solve this problem, we can follow these steps:

1. Calculate the area of the pentagon using the formula: `¼ * √5 * (5 + 2 * √5) * side²`.
2. Use the provided value for the side length to compute the area.

## Pseudocode

``````function pentagon_area(side):
area = (1/4) * sqrt(5 * (5 + 2 * sqrt(5))) * side * side
return area

for each test case:
side = pentagon side length
result = pentagon_area(side)
print("Pentagon [ Side :", side, "]")
print("Area :", result)``````

## Algorithm Explanation

1. For each test case, calculate the area of the pentagon using the provided formula.
2. Print the pentagon's side length and the calculated area.

## Code Solution

``````//C Program
//Find the Area of Pentagon
#include <stdio.h>

#include <math.h>
//Calculate area of pentagon by given side
void pentagon_area(double side)
{
//Calculate Formula
//¼√5(5+2√5)side²
double area = ((double) 1 / 4) * (sqrt(5 * (5 + 2 * (sqrt(5)))) * (side * side));
//Display the result of pentagon area
printf(" Pentagon  [ Side : %lf ] ", side);
printf("\n Area : %lf\n\n", area);
}
int main()
{
//Test Cases
pentagon_area(6);
pentagon_area(4);
pentagon_area(3.5);
return 0;
}``````

#### Output

`````` Pentagon  [ Side : 6.000000 ]
Area : 61.937186

Pentagon  [ Side : 4.000000 ]
Area : 27.527638

Pentagon  [ Side : 3.500000 ]
Area : 21.075848
``````
``````// Java Program
// Find the area of Pentagon
class PentagonArea
{
//Calculate area of pentagon by given side
public void pentagon_area(double side)
{
//Calculate Formula
//¼√5(5+2√5)side²
double area = ((double) 1 / 4) * (Math.sqrt(5 * (5 + 2 * (Math.sqrt(5)))) * (side * side));
//Display the result of pentagon area
System.out.print(" Pentagon [ Side : " + side + " ] ");
System.out.print("\n Area : " + area + "\n\n");
}
public static void main(String[] args)
{
PentagonArea obj = new PentagonArea();
//Test Cases
obj.pentagon_area(6);
obj.pentagon_area(4);
obj.pentagon_area(3.5);
}
}``````

#### Output

`````` Pentagon [ Side : 6.0 ]
Area : 61.93718642120281

Pentagon [ Side : 4.0 ]
Area : 27.52763840942347

Pentagon [ Side : 3.5 ]
Area : 21.075848157214846
``````
``````// C++ Program
// Find the area of Pentagon
#include<iostream>
#include <math.h>
using namespace std;
class PentagonArea
{
public:
//Calculate area of pentagon by given side
void pentagon_area(double side)
{
//Calculate Formula
//¼√5(5+2√5)side²
double area = ((double) 1 / 4) *(sqrt(5 *(5 + 2 *(sqrt(5)))) *(side *side));
cout << " Pentagon [ Side : " << side << " ] ";
cout << "\n Area : " << area << "\n\n";
}
};
int main()
{
PentagonArea obj = PentagonArea();
//Test Cases
obj.pentagon_area(6);
obj.pentagon_area(4);
obj.pentagon_area(3.5);
return 0;
}``````

#### Output

`````` Pentagon [ Side : 6 ]
Area : 61.9372

Pentagon [ Side : 4 ]
Area : 27.5276

Pentagon [ Side : 3.5 ]
Area : 21.0758
``````
``````// C# Program
// Find the area of Pentagon
using System;
class PentagonArea
{
//Calculate area of pentagon by given side
public void pentagon_area(double side)
{
//Calculate Formula
//¼√5(5+2√5)side²
double area = ((double) 1 / 4) * (Math.Sqrt(5 * (5 + 2 * (Math.Sqrt(5)))) * (side * side));
Console.Write(" Pentagon [ Side : " + side + " ] ");
Console.Write("\n Area : " + area + "\n\n");
}
public static void Main(String[] args)
{
PentagonArea obj = new PentagonArea();
//Test Cases
obj.pentagon_area(6);
obj.pentagon_area(4);
obj.pentagon_area(3.5);
}
}``````

#### Output

`````` Pentagon [ Side : 6 ]
Area : 61.9371864212028

Pentagon [ Side : 4 ]
Area : 27.5276384094235

Pentagon [ Side : 3.5 ]
Area : 21.0758481572148
``````
``````<?php
// Php Program
// Find the area of Pentagon
class PentagonArea
{
//Calculate area of pentagon by given side
public 	function pentagon_area(\$side)
{
//Calculate Formula
//¼√5(5+2√5)side²
\$area = (1 / 4) *(sqrt(5 *(5 + 2 *(sqrt(5)))) *(\$side *\$side));
//Display the result of pentagon area
echo(" Pentagon [ Side : ". \$side ." ] ");
echo("\n Area : ". \$area ."\n\n");
}
}

function main()
{
\$obj = new PentagonArea();
//Test Cases
\$obj->pentagon_area(6);
\$obj->pentagon_area(4);
\$obj->pentagon_area(3.5);
}
main();``````

#### Output

`````` Pentagon [ Side : 6 ]
Area : 61.937186421203

Pentagon [ Side : 4 ]
Area : 27.527638409423

Pentagon [ Side : 3.5 ]
Area : 21.075848157215
``````
``````// Node Js Program
// Find the area of Pentagon
class PentagonArea
{
//Calculate area of pentagon by given side
pentagon_area(side)
{
//Calculate Formula
//¼√5(5+2√5)side²
var area = (1 / 4) *(Math.sqrt(5 *(5 + 2 *(Math.sqrt(5)))) *(side *side));
//Display the result of pentagon area
process.stdout.write(" Pentagon [ Side : " + side + " ] ");
process.stdout.write("\n Area : " + area + "\n\n");
}
}

function main(args)
{
var obj = new PentagonArea();
//Test Cases
obj.pentagon_area(6);
obj.pentagon_area(4);
obj.pentagon_area(3.5);
}
main();``````

#### Output

`````` Pentagon [ Side : 6 ]
Area : 61.93718642120281

Pentagon [ Side : 4 ]
Area : 27.52763840942347

Pentagon [ Side : 3.5 ]
Area : 21.075848157214846
``````
``````#  Python 3 Program
#  Find the area of Pentagon
import math
class PentagonArea :
# Calculate area of pentagon by given side
def pentagon_area(self, side) :
# Calculate Formula
# ¼√5(5+2√5)side²
area = (1 / 4) * (math.sqrt(5 * (5 + 2 * (math.sqrt(5)))) * (side * side))
# Display the result of pentagon area
print(" Pentagon [ Side : ", side ," ] ", end = "")
print("\n Area : ", area ,"\n\n", end = "")

def main() :
obj = PentagonArea()
# Test Cases
obj.pentagon_area(6)
obj.pentagon_area(4)
obj.pentagon_area(3.5)

if __name__ == "__main__": main()``````

#### Output

`````` Pentagon [ Side :  6  ]
Area :  61.93718642120281

Pentagon [ Side :  4  ]
Area :  27.52763840942347

Pentagon [ Side :  3.5  ]
Area :  21.075848157214846
``````
``````#  Ruby Program
#  Find the area of Pentagon
class PentagonArea

# Calculate area of pentagon by given side
def pentagon_area(side)

# Calculate Formula
# ¼√5(5+2√5)side²
# 1/4 = 0.25
area = (0.25) * (Math.sqrt(5 * (5 + 2 * (Math.sqrt(5)))) * (side * side))
# Display the result of pentagon area
print(" Pentagon [ Side  : ", side ," ] ")
print("\n Area  : ", area ,"\n\n")
end
end
def main()

obj = PentagonArea.new()
# Test Cases
obj.pentagon_area(6)
obj.pentagon_area(4)
obj.pentagon_area(3.5)
end
main()``````

#### Output

`````` Pentagon [ Side  : 6 ]
Area  : 61.93718642120281

Pentagon [ Side  : 4 ]
Area  : 27.52763840942347

Pentagon [ Side  : 3.5 ]
Area  : 21.075848157214846

``````
``````// Scala Program
// Find the area of Pentagon
class PentagonArea
{
//Calculate area of pentagon by given side
def pentagon_area(side: Double): Unit = {
//Calculate Formula
//¼√5(5+2√5)side²
//1/4 = 0.25
var area: Double = (0.25) * (Math.sqrt(5 * (5 + 2 * (Math.sqrt(5)))) * (side * side));
//Display the result of pentagon area
print(" Pentagon [ Side : " + side + " ] ");
print("\n Area : " + area + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: PentagonArea = new PentagonArea();
//Test Cases
obj.pentagon_area(6);
obj.pentagon_area(4);
obj.pentagon_area(3.5);
}
}``````

#### Output

`````` Pentagon [ Side : 6.0 ]
Area : 61.93718642120281

Pentagon [ Side : 4.0 ]
Area : 27.52763840942347

Pentagon [ Side : 3.5 ]
Area : 21.075848157214846
``````
``````// Swift Program
// Find the area of Pentagon
import Foundation
class PentagonArea
{
//Calculate area of pentagon by given side
func pentagon_area(_ side: Double)
{
//Calculate Formula
//¼√5(5+2√5)side²
//1/4 = 0.25
let area: Double = (0.25) * (sqrt(5 * (5 + 2 * (sqrt(5)))) * (side * side));
//Display the result of pentagon area
print(" Pentagon [ Side : ", side ," ] ");
print(" Area : ", area ,"\n");
}
}
func main()
{
let obj: PentagonArea = PentagonArea();
//Test Cases
obj.pentagon_area(6);
obj.pentagon_area(4);
obj.pentagon_area(3.5);
}
main();``````

#### Output

`````` Pentagon [ Side :  6.0  ]
Area :  61.9371864212028

Pentagon [ Side :  4.0  ]
Area :  27.5276384094235

Pentagon [ Side :  3.5  ]
Area :  21.0758481572148
``````

## Output Explanation

The provided code runs three test cases and calculates the areas of regular pentagons with different side lengths. The output includes the pentagon's side length and the corresponding calculated areas.

## Time Complexity

The time complexity of this code is O(1) for each test case. The calculations involve basic arithmetic operations and square root computations, which take constant time. Therefore, the overall time complexity remains constant for each test case.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post