Length of the Diagonal of the Octagon - GeeksforGeeks

17 Mar, 2021
Given here is a regular octagon of side length a, the task is to find the length of it’s diagonal.Examples:  
Input: a = 4
Output: 10.4525

Input: a = 5
Output: 13.0656
 
 
Approach: From the diagram it is clear that, 
AB^2 + BC^2 = AC^2 here, in triangle AED, b^2 + b^2 = a^2 or, b=a/√2(Please refer) So, AB = a + 2b = a + √2a and, BC = a So, diagonal AC = a√(4 + 2√2)
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the diagonal// of the octagon#include <bits/stdc++.h>using namespace std; // Function to find the diagonal// of the octagonfloat octadiagonal(float a){     // side cannot be negative    if (a < 0)        return -1;     // diagonal of the octagon    return a * sqrt(4 + (2 * sqrt(2)));} // Driver codeint main(){    float a = 4;    cout << octadiagonal(a) << endl;     return 0;}
// Java  Program to find the diagonal// of the octagonimport java.util.*;class solution{   // Function to find the diagonal// of the octagonstatic double octadiagonal(double a){       // side cannot be negative    if (a < 0)        return -1;       // diagonal of the octagon    return a * Math.sqrt(4 + (2 * Math.sqrt(2)));}   // Driver codepublic static void main(String args[]){    double a = 4;    System.out.println( octadiagonal(a));   }}//contributed by Arnab Kundu
# Python3 Program to find the diagonal# of the octagonimport math# Function to find the diagonal# of the octagondef octadiagonal(a):     # side cannot be negative    if (a < 0):        return -1;     # diagonal of the octagon    return a * math.sqrt(4 + (2 * math.sqrt(2)))  # Driver codeif __name__=='__main__':    a = 4    print (octadiagonal(a)) # This code is contributed by# Shivi_Aggarwal
// C# Program to find the diagonal// of the octagonusing System; class GFG{ // Function to find the diagonal// of the octagonstatic double octadiagonal(double a){     // side cannot be negative    if (a < 0)        return -1;     // diagonal of the octagon    return a * Math.Sqrt(4 +          (2 * Math.Sqrt(2)));} // Driver codepublic static void Main(){    double a = 4;    Console.WriteLine(octadiagonal(a));}} // This code is contributed// by inder_verma
<?php// PHP program to find the diagonal// of the octagon // Function to find the diagonal// of the octagonfunction octadiagonal($a){     // side cannot be negative    if ($a < 0)        return -1;     // diagonal of the octagon    return $a * sqrt(4 + (2 * sqrt(2)));} // Driver code     $a = 4;    echo octadiagonal($a) ; // This code is contributed// by inder_verma ?>
<script>// javascript  Program to find the diagonal// of the octagon // Function to find the diagonal// of the octagonfunction octadiagonal(a){       // side cannot be negative    if (a < 0)        return -1;       // diagonal of the octagon    return a * Math.sqrt(4 + (2 * Math.sqrt(2)));} var a = 4;document.write( octadiagonal(a).toFixed(5)); // This code is contributed by 29AjayKumar</script>
10.4525
 
andrew1234
inderDuMCA
Shivi_Aggarwal
29AjayKumar
Geometric
Mathematical
School Programming
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Convex Hull | Set 2 (Graham Scan)
Optimum location of point to minimize total distance
Check whether a given point lies inside a triangle or not
Program for Fibonacci numbers
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
C++ Data Types
Coin Change | DP-7