Posted on by Kalkicode
Code Geometric

# Find the area of a Octagon

An octagon is an eight-sided polygon with equal-length sides. Finding the area of an octagon involves determining the space enclosed by its eight sides. This calculation is essential in various fields, including geometry, architecture, and design. To compute the area, we can utilize the side length of the octagon. This post will explain how to find the area of an octagon using a given side length.

## Problem Statement

Given the side length of an octagon, the task is to calculate its area. The formula for finding the area of an octagon in terms of its side length 's' is:

Area = 2 * s^2 * (1 + √2)

## Example Scenario

Imagine you are an architect working on designing a decorative element for a building. You want to create an octagonal panel to fit within a certain space. You know the side length of the octagon, and you need to calculate its area to estimate the material required for its construction. This is where the area calculation comes into play.

## Idea to Solve the Problem

To solve this problem, we can follow these steps:

1. Accept the side length of the octagon as input.
2. Use the formula to calculate the area of the octagon.
3. Display the calculated area.

## Pseudocode

``````function octagon_area(side):
area = 2 * side^2 * (1 + √2)
return area

main:
side1 = 6
side2 = 5
side3 = 5.2

area1 = octagon_area(side1)
area2 = octagon_area(side2)
area3 = octagon_area(side3)

print("Octagon Area of side", side1, "is:", area1)
print("Octagon Area of side", side2, "is:", area2)
print("Octagon Area of side", side3, "is:", area3)``````

## Algorithm Explanation

1. Define a function `octagon_area` that takes the side length as input.
2. Inside the function, use the provided formula to calculate the area of the octagon.
3. In the `main` function, set three test cases with different side lengths: 6, 5, and 5.2.
4. Calculate the areas for each test case by calling the `octagon_area` function.
5. Display the calculated areas along with their respective side lengths.

## Code Solution

``````/*
C Program
Find the area of a Octagon
*/
#include <stdio.h>

#include <math.h>

//Calculate area of octagon by given side length
void octagon_area(double side)
{
// Formula of octagon area
// ( 2 × (side)² × (1+√2)
// Calculate area of octagon
double area = 2 * (side * side) * (1 + sqrt(2));
//Display result
printf("Octagon Area of side %lf is : %lf\n", side, area);
}
int main()
{
//Test Case
octagon_area(6);
octagon_area(5);
octagon_area(5.2);
return 0;
}``````

#### Output

``````Octagon Area of side 6.000000 is : 173.823376
Octagon Area of side 5.000000 is : 120.710678
Octagon Area of side 5.200000 is : 130.560669``````
``````// Java Program
// Find the area of a octagon
class Octagon
{
//Calculate area of octagon by given side length
public void octagon_area(double side)
{
// Formula of octagon area
// ( 2 × (side)² × (1+√2)
// Calculate area of octagon
double area = 2 * (side * side) * (1 + Math.sqrt(2));
System.out.print("Octagon Area of side length " + side + " is : " + area + "\n");
}
public static void main(String[] args)
{
Octagon obj = new Octagon();
//Simple Case
obj.octagon_area(6);
obj.octagon_area(5);
obj.octagon_area(5.2);
}
}``````

#### Output

``````Octagon Area of side length 6.0 is : 173.82337649086284
Octagon Area of side length 5.0 is : 120.71067811865474
Octagon Area of side length 5.2 is : 130.560669453137``````
``````// C++ Program
// Find the area of a octagon
#include<iostream>
#include<math.h>
using namespace std;
class Octagon
{
public:
//Calculate area of octagon by given side length
void octagon_area(double side)
{
// Formula of octagon area
// ( 2 × (side)² × (1+√2)
// Calculate area of octagon
double area = 2 * (side * side) * (1 + sqrt(2));
cout << "Octagon Area of side length " << side << " is : " << area << "\n";
}
};
int main()
{
Octagon obj =  Octagon();
//Simple Case
obj.octagon_area(6);
obj.octagon_area(5);
obj.octagon_area(5.2);
return 0;
}``````

#### Output

``````Octagon Area of side length 6 is : 173.823
Octagon Area of side length 5 is : 120.711
Octagon Area of side length 5.2 is : 130.561``````
``````// C# Program
// Find the area of a octagon
using System;
class Octagon
{
//Calculate area of octagon by given side length
public void octagon_area(double side)
{
// Formula of octagon area
// ( 2 × (side)² × (1+√2)
// Calculate area of octagon
double area = 2 * (side * side) * (1 + Math.Sqrt(2));
Console.Write("Octagon Area of side length " + side + " is : " + area + "\n");
}
public static void Main(String[] args)
{
Octagon obj = new Octagon();
//Simple Case
obj.octagon_area(6);
obj.octagon_area(5);
obj.octagon_area(5.2);
}
}``````

#### Output

``````Octagon Area of side length 6 is : 173.823376490863
Octagon Area of side length 5 is : 120.710678118655
Octagon Area of side length 5.2 is : 130.560669453137``````
``````<?php
// Php Program
// Find the area of a octagon
class Octagon
{
//Calculate area of octagon by given side length
public	function octagon_area(\$side)
{
// Formula of octagon area
// ( 2 × (side)² × (1+√2)
// Calculate area of octagon
\$area = 2 * (\$side * \$side) * (1 + sqrt(2));
echo "Octagon Area of side length ". \$side ." is : ". \$area ."\n";
}
}

function main()
{
\$obj = new Octagon();
//Simple Case
\$obj->octagon_area(6);
\$obj->octagon_area(5);
\$obj->octagon_area(5.2);
}
main();``````

#### Output

``````Octagon Area of side length 6 is : 173.82337649086
Octagon Area of side length 5 is : 120.71067811865
Octagon Area of side length 5.2 is : 130.56066945314``````
``````// Node Js Program
// Find the area of a octagon
class Octagon
{
//Calculate area of octagon by given side length
octagon_area(side)
{
// Formula of octagon area
// ( 2 × (side)² × (1+√2)
// Calculate area of octagon
var area = 2 * (side * side) * (1 + Math.sqrt(2));
process.stdout.write("Octagon Area of side length " + side + " is : " + area + "\n");
}
}

function main()
{
var obj = new Octagon();
//Simple Case
obj.octagon_area(6);
obj.octagon_area(5);
obj.octagon_area(5.2);
}
main();``````

#### Output

``````Octagon Area of side length 6 is : 173.82337649086284
Octagon Area of side length 5 is : 120.71067811865474
Octagon Area of side length 5.2 is : 130.560669453137``````
``````#  Python 3 Program
#  Find the area of a octagon

import math

class Octagon :
# Calculate area of octagon by given side length
def octagon_area(self, side) :
#  Formula of octagon area
#  ( 2 × (side)² × (1+√2)
#  Calculate area of octagon
area = 2 * (side * side) * (1 + math.sqrt(2))
print("Octagon Area of side length ", side ," is : ", area ,"\n", end = "")

def main() :
obj = Octagon()
# Simple Case
obj.octagon_area(6)
obj.octagon_area(5)
obj.octagon_area(5.2)

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

#### Output

``````Octagon Area of side length  6  is :  173.82337649086284
Octagon Area of side length  5  is :  120.71067811865474
Octagon Area of side length  5.2  is :  130.560669453137``````
``````#  Ruby Program
#  Find the area of a octagon
class Octagon

# Calculate area of octagon by given side length
def octagon_area(side)

#  Formula of octagon area
#  ( 2 × (side)² × (1+√2)
#  Calculate area of octagon
area = 2 * (side * side) * (1 + Math.sqrt(2))
print("Octagon Area of side length ", side ," is : ", area ,"\n")
end
end
def main()

obj = Octagon.new()
# Simple Case
obj.octagon_area(6)
obj.octagon_area(5)
obj.octagon_area(5.2)
end
main()``````

#### Output

``````Octagon Area of side length 6 is : 173.82337649086284
Octagon Area of side length 5 is : 120.71067811865474
Octagon Area of side length 5.2 is : 130.560669453137
``````
``````// Scala Program
// Find the area of a octagon
class Octagon
{
//Calculate area of octagon by given side length
def octagon_area(side: Double): Unit = {
// Formula of octagon area
// ( 2 × (side)² × (1+√2)
// Calculate area of octagon
var area: Double = 2 * (side * side) * (1 + Math.sqrt(2));
print("Octagon Area of side length " + side + " is : " + area + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: Octagon = new Octagon();
//Simple Case
obj.octagon_area(6);
obj.octagon_area(5);
obj.octagon_area(5.2);
}
}``````

#### Output

``````Octagon Area of side length 6.0 is : 173.82337649086284
Octagon Area of side length 5.0 is : 120.71067811865474
Octagon Area of side length 5.2 is : 130.560669453137``````
``````// Swift Program
// Find the area of a octagon

import Foundation

class Octagon
{
//Calculate area of octagon by given side length
func octagon_area(_ side: Double)
{
// Formula of octagon area
// ( 2 × (side)² × (1+√2)
// Calculate area of octagon
let area: Double = 2 * (side * side) * (1 + sqrt(2));
print("Octagon Area of side length ", side ," is : ", area ,"\n", terminator: "");
}
}
func main()
{
let obj: Octagon = Octagon();
//Simple Case
obj.octagon_area(6);
obj.octagon_area(5);
obj.octagon_area(5.2);
}
main();``````

#### Output

``````Octagon Area of side length  6.0  is :  173.823376490863
Octagon Area of side length  5.0  is :  120.710678118655
Octagon Area of side length  5.2  is :  130.560669453137``````

## Output Explanation

The code calculates the area for each test case and displays the results. For a side length of 6, the area is approximately 173.823376. Similarly, for side lengths of 5 and 5.2, the areas are approximately 120.710678 and 130.560669, respectively.

## Time Complexity

The time complexity of this code is constant O(1) because the calculations involve basic arithmetic operations, which take a constant amount of time regardless of the input size. The program performs a fixed number of operations for each test case, making it efficient.

## 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