# Program to find the shortest distance between diagonal and edge skew of a Cube

Given an integer **A**, denoting the length of a cube, the task is to find the shortest distance between the diagonal of a cube and an edge skew to it i.e. **KL** in the below figure.

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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

**Examples :**

Input:A = 2Output:1.4142Explanation:

Length of KL = A / √2

Length of KL = 2 / 1.41 = 1.4142

Input:A = 3Output:2.1213

**Approach:** The idea to solve the problem is based on the following mathematical formula:

Let’s draw a perpendicular from K to down face of cube as Q.

Using Pythagorean Theorem in triangle QKL,KL

^{2}= QK^{2}+ QL^{2}l

^{2}= (a/2)^{2}+ (a/2)^{2}l

^{2}= 2 * (a/2)^{2}

l = a / sqrt(2)

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the shortest distance` `// between the diagonal of a cube and` `// an edge skew to it` `float` `diagonalLength(` `float` `a)` `{` ` ` `// Stores the required distance` ` ` `float` `L = a / ` `sqrt` `(2);` ` ` `// Print the required distance` ` ` `cout << L;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given side of the cube` ` ` `float` `a = 2;` ` ` `// Function call to find the shortest` ` ` `// distance between the diagonal of a` ` ` `// cube and an edge skew to it` ` ` `diagonalLength(a);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `class` `GFG {` ` ` `// Function to find the shortest` ` ` `// distance between the diagonal of a` ` ` `// cube and an edge skew to it` ` ` `static` `void` `diagonalLength(` `float` `a)` ` ` `{` ` ` `// Stores the required distance` ` ` `float` `L = a / (` `float` `)Math.sqrt(` `2` `);` ` ` `// Print the required distance` ` ` `System.out.println(L);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `// Given side of the cube` ` ` `float` `a = ` `2` `;` ` ` `// Function call to find the shortest` ` ` `// distance between the diagonal of a` ` ` `// cube and an edge skew to it` ` ` `diagonalLength(a);` ` ` `}` `}` |

## Python3

`# Python3 program for the above approach` `from` `math ` `import` `sqrt` `# Function to find the shortest` `# distance between the diagonal of a` `# cube and an edge skew to it` `def` `diagonalLength(a):` ` ` ` ` `# Stores the required distance` ` ` `L ` `=` `a ` `/` `sqrt(` `2` `)` ` ` `# Print the required distance` ` ` `print` `(L)` `# Given side of the cube` `a ` `=` `2` `# Function call to find the shortest` `# distance between the diagonal of a` `# cube and an edge skew to it` `diagonalLength(a)` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG {` ` ` `// Function to find the shortest` ` ` `// distance between the diagonal of a` ` ` `// cube and an edge skew to it` ` ` `static` `void` `diagonalLength(` `float` `a)` ` ` `{` ` ` `// Stores the required distance` ` ` `float` `L = a / (` `float` `)Math.Sqrt(2);` ` ` `// Print the required distance` ` ` `Console.Write(L);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `// Given side of the cube` ` ` `float` `a = 2;` ` ` `// Function call to find the shortest` ` ` `// distance between the diagonal of a` ` ` `// cube and an edge skew to it` ` ` `diagonalLength(a);` ` ` `}` `}` |

## PHP

`<?php` `// PHP program for the above approach` ` ` `// Function to find the shortest` `// distance between the diagonal of a` `// cube and an edge skew to it` `function` `diagonalLength(` `$a` `)` `{` ` ` `// Stores the required distance` ` ` `$L` `= ` `$a` `/ sqrt(2);` ` ` `# ` `Print` `the required distance` ` ` `echo` `$L` `;` `}` `// Given side of the cube` `$a` `= 2;` `// Function call to find the shortest` `// distance between the diagonal of a` `// cube and an edge skew to it` `diagonalLength(` `$a` `);` `?>` |

## Javascript

`<script>` `// javascript program for the above approach` `// Function to find the shortest distance` `// between the diagonal of a cube and` `// an edge skew to it` `function` `diagonalLength( a)` `{` ` ` `// Stores the required distance` ` ` `let L = a / Math.sqrt(2);` ` ` `// Print the required distance` ` ` `document.write( L.toFixed(5));` `}` `// Driver Code` ` ` `// Given side of the cube` ` ` `let a = 2;` ` ` `// Function call to find the shortest` ` ` `// distance between the diagonal of a` ` ` `// cube and an edge skew to it` ` ` `diagonalLength(a);` `// This code is contributed by todaysgaurav` `</script>` |

**Output:**

1.41421

**Time Complexity:** O(1)**Auxiliary Space:** O(1)