Program to find the side of the Octagon inscribed within the square
Last Updated :
11 Aug, 2022
Given a square of side length 'a', the task is to find the side length of the biggest octagon that can be inscribed within it.
Examples:
Input: a = 4
Output: 1.65685
Input: a = 5
Output: 2.07107

Approach:
=> From the figure, it can be seen that, side length of the Octagon = b
=> Also since the polygons are regular, therefore 2*x + b = a
=> From the right angled triangle, x^2 + x^2 = b^2
=> Hence, x = b/?2,
=> So, ?2b + b = a
=> Therefore, b = a/(?2 +1)
Below is the implementation of the above approach:
C++
// C++ Program to find the side of the octagon
// which can be inscribed within the square
#include <bits/stdc++.h>
using namespace std;
// Function to find the side
// of the octagon
float octaside(float a)
{
// side cannot be negative
if (a < 0)
return -1;
// side of the octagon
float s = a / (sqrt(2) + 1);
return s;
}
// Driver code
int main()
{
// Get he square side
float a = 4;
// Find the side length of the square
cout << octaside(a) << endl;
return 0;
}
Java
// Java Program to find the side of the octagon
// which can be inscribed within the square
import java.io.*;
class GFG {
// Function to find the side
// of the octagon
static double octaside(double a)
{
// side cannot be negative
if (a < 0)
return -1;
// side of the octagon
double s = a / (Math.sqrt(2) + 1);
return s;
}
// Driver code
public static void main (String[] args) {
// Get he square side
double a = 4;
// Find the side length of the square
System.out.println( octaside(a));
}
}
//This Code is contributed by ajit
Python3
# Python 3 Program to find the side
# of the octagon which can be
# inscribed within the square
from math import sqrt
# Function to find the side
# of the octagon
def octaside(a):
# side cannot be negative
if a < 0:
return -1
# side of the octagon
s = a / (sqrt(2) + 1)
return s
# Driver code
if __name__ == '__main__':
# Get he square side
a = 4
# Find the side length of the square
print("{0:.6}".format(octaside(a)))
# This code is contributed
# by Surendra_Gangwar
C#
// C# Program to find the side
// of the octagon which can be
// inscribed within the square
using System;
class GFG
{
// Function to find the side
// of the octagon
static double octaside(double a)
{
// side cannot be negative
if (a < 0)
return -1;
// side of the octagon
double s = a / (Math.Sqrt(2) + 1);
return s;
}
// Driver code
static public void Main ()
{
// Get he square side
double a = 4;
// Find the side length
// of the square
Console.WriteLine( octaside(a));
}
}
// This code is contributed
// by akt_mit
PHP
<?php
// PHP Program to find the side of the octagon
// which can be inscribed within the square
// Function to find the side
// of the octagon
function octaside($a)
{
// side cannot be negative
if ($a < 0)
return -1;
// side of the octagon
$s = $a / (sqrt(2) + 1);
return $s;
}
// Driver code
// Get he square side
$a = 4;
// Find the side length of the square
echo octaside($a);
// This code is contributed by ajit
?>
JavaScript
<script>
// javascript Program to find the side of the octagon
// which can be inscribed within the square
// Function to find the side
// of the octagon
function octaside(a)
{
// side cannot be negative
if (a < 0)
return -1;
// side of the octagon
var s = a / (Math.sqrt(2) + 1);
return s;
}
// Driver code
// Get he square side
var a = 4;
// Find the side length of the square
document.write( octaside(a).toFixed(5));
// This code is contributed by shikhasingrajput
</script>
Time Complexity: O(1) since no loop is used the algorithm takes up constant time to perform the operations
Space Complexity: O(1) since no extra array is used so the space taken by the algorithm is constant
Similar Reads
Find the side of the squares which are inclined diagonally and lined in a row Given here are n squares which are inclined and touch each other externally at vertices, and are lined up in a row.The distance between the centers of the first and last square is given.The squares have equal side length.The task is to find the side of each square.Examples: Input :d = 42, n = 4 Outp
4 min read
Find area of the Circle when the area of inscribed Square is given Given the area of a square inscribed in a circle as N, the task is to calculate the area of a circle in which the square is inscribed. Examples: Input: N = 4 Output: 6.283 Input: N = 10Output: 15.707 Approach: Consider the below image: Let the area of the square is 'A'The side of the square is given
4 min read
Program to find the area of a Square What is a square ? Square is a flat shape, in one plane, defined by four points at the four corners. A square has four sides all of equal length, and four corners, all right angles (90 degree angles). A square is a kind of rectangle but with all sides equal. Formula : Area = side * side Examples: In
3 min read
Largest Square that can be inscribed within a hexagon Given a regular hexagonof side length a, the task is to find the area of the largest square that can be inscribed within it.Examples: Input: a = 6 Output: 57.8817Input: a = 8 Output: 102.901 Approach: The square we will derive will have the same centre and axes of the hexagon. This is because the sq
4 min read
Program to determine the octant of the axial plane Given 3 coordinates x, y and z, the task is to determine the octant of the axial plane.Examples: Input: 2, 3, 4 Output: Point lies in 1st octant Input: -4, 2, -8 Output: Point lies in 6th octant Input: -6, -2, 8 Output: Point lies in 3rd octant Approach: Given below are the conditions which need to
10 min read
Find Four points such that they form a square whose sides are parallel to x and y axes Given 'n' pair of points, the task is to find four points such that they form a square whose sides are parallel to x and y axes or print "No such square" otherwise. If more than one square is possible then choose the one with the maximum area. Examples: Input : n = 6, points = (1, 1), (4, 4), (3, 4)
12 min read