Given a side of the cube **a**. The task is to find the length of the diagonal of the cube.

**Examples:**

Input :a = 3

Output :5.19615

Input :a = 6

Output :10.3923

**Formula : **

Length of diagonal of the cube = sqrt(3) * side

**Proof :**

Use Pythagorean Theorem,

In triangle CED,

CE^{2}= CD^{2}+ DE^{2}

l^{2}= a^{2}+ a^{2}———>(1)

In triangle CFE,

CF^{2}= CE^{2}+ EF^{2}

L^{2}= l^{2}+ a^{2}

use l^{2}value from equation(1),

L^{2}= a^{2}+ a^{2}+ a^{2}

= 3*a^{2}

L = sqrt(3) * a

## C++

`// CPP program to find length ` `// of the diagonal of the cube ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find length ` `// of diagonal of cube ` `float` `diagonal_length(` `float` `a) ` `{ ` ` ` `float` `L; ` ` ` ` ` `// Formula to Find length ` ` ` `// of diagonal of cube ` ` ` `L = a * ` `sqrt` `(3); ` ` ` ` ` `return` `L; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `float` `a = 5; ` ` ` ` ` `// Function call ` ` ` `cout << diagonal_length(a); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find length ` `// of the diagonal of the cube ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find length ` ` ` `// of diagonal of cube ` ` ` `static` `float` `diagonal_length(` `float` `a) ` ` ` `{ ` ` ` `float` `L; ` ` ` ` ` `// Formula to Find length ` ` ` `// of diagonal of cube ` ` ` `L = a * (` `float` `)Math.sqrt(` `3` `); ` ` ` ` ` `return` `L; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `float` `a = ` `5` `; ` ` ` ` ` `// Function call ` ` ` `System.out.println(diagonal_length(a)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find length ` `# of the diagonal of the cube ` `from` `math ` `import` `sqrt ` ` ` `# Function to find length ` `# of diagonal of cube ` `def` `diagonal_length(a): ` ` ` `L ` `=` `0` ` ` ` ` `# Formula to Find length ` ` ` `# of diagonal of cube ` ` ` `L ` `=` `a ` `*` `sqrt(` `3` `) ` ` ` ` ` `return` `L ` ` ` `# Driver code ` `a ` `=` `5` ` ` `# Function call ` `print` `(diagonal_length(a)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find length ` `// of the diagonal of the cube ` `using` `System; ` `class` `GFG ` `{ ` `// Function to find length ` `// of diagonal of cube ` `static` `float` `diagonal_length(` `float` `a) ` `{ ` ` ` `float` `L; ` ` ` ` ` `// Formula to Find length ` ` ` `// of diagonal of cube ` ` ` `L = a * (` `float` `)Math.Sqrt(3); ` ` ` ` ` `return` `L; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `float` `a = 5; ` ` ` ` ` `// Function call ` ` ` `Console.Write(diagonal_length(a)); ` `} ` `} ` ` ` `// This code is contributed by Nidhi ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find length ` `// of the diagonal of the cube ` ` ` `// Function to find length ` `// of diagonal of cube ` `function` `diagonal_length(` `$a` `) ` `{ ` ` ` `$L` `; ` ` ` ` ` `// Formula to Find length ` ` ` `// of diagonal of cube ` ` ` `$L` `= ` `$a` `* sqrt(3); ` ` ` ` ` `return` `$L` `; ` `} ` ` ` `// Driver code ` `$a` `= 5; ` ` ` `// Function call ` `echo` `diagonal_length(` `$a` `); ` ` ` `// This code is contributed ` `// by Naman_Garg. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

8.66025

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Volume of cube using its space diagonal
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Find Cube Pairs | Set 1 (A n^(2/3) Solution)
- Find Cube Pairs | Set 2 (A n^(1/3) Solution)
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find Cube root of a number using Log function
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Find length of Diagonal of Hexagon
- Find the area of quadrilateral when diagonal and the perpendiculars to it from opposite vertices are given
- Centered cube number
- Program for cube sum of first n natural numbers
- Program for Volume and Surface Area of Cube
- N-th number which is both a square and a cube
- Cube Free Numbers smaller than n
- Puzzle | Cut Blue Painted Cube
- Perfect cube greater than a given number
- Largest cube that can be inscribed within the sphere
- Largest sphere that can be inscribed inside a cube
- Largest cone that can be inscribed within a cube

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.