Program to calculate Area Of Octagon
Last Updated :
17 Feb, 2023
A regular octagon is a closed figure with sides of the same length and internal angles of the same size. It has eight lines of reflective symmetry and rotational symmetry of order 8. The internal angle at each vertex of a regular octagon is 135°. The central angle is 45°.
Properties :
Convex polygon, Equilateral polygon, Isogonal figure, Isotoxal figure, Cyclic.
Formula :
Area : 2 × (side length)² × (1+sqrt(2))
Examples :
Input : side = 3
Output : Area of Regular Octagon = 43.4558
Input : side = 4
Output : Area of Regular Octagon = 77.2548
C++
#include <bits/stdc++.h>
using namespace std;
double areaOctagon( double side)
{
return ( float )(2 * (1 + sqrt (2)) *
side * side);
}
int main()
{
double side = 4;
cout << "Area of Regular Octagon = "
<< areaOctagon(side) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static double areaOctagon( double side)
{
return ( float )( 2 * ( 1 + Math.sqrt( 2 ))
* side * side);
}
public static void main(String arg[])
{
double side = 4 ;
System.out.print( "Area of Regular Octagon = "
+ areaOctagon(side));
}
}
|
Python3
import math
def areaOctagon(side):
return ( 2 * ( 1 + (math.sqrt( 2 ))) * side * side)
side = 4
print ( "Area of Regular Octagon =" ,
round (areaOctagon(side), 4 ))
|
C#
using System;
class GFG
{
static double areaOctagon( double side)
{
return ( float )(2 * (1 + Math.Sqrt(2))
* side * side);
}
public static void Main()
{
double side = 4;
Console.WriteLine( "Area of Regular Octagon = "
+ areaOctagon(side));
}
}
|
PHP
<?php
function areaOctagon( $side )
{
return (2 * (1 + sqrt(2)) *
$side * $side );
}
$side = 4;
echo ( "Area of Regular Octagon = " );
echo (areaOctagon( $side ));
?>
|
Javascript
<script>
function areaOctagon(side)
{
return (2 * (1 + Math.sqrt(2)) *
side * side);
}
let side = 4;
document.write( "Area of Regular Octagon = "
+ areaOctagon(side) + "<br>" );
</script>
|
Output :
Area of Regular Octagon = 77.25
Time complexity : O(1)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...