Program to calculate length of diagonal of a square Last Updated : 29 Apr, 2021 Comments Improve Suggest changes Like Article Like Report Given a positive integer S, the task is to find the length of diagonal of a square having sides of length S. Examples: Input: S = 10Output: 14.1421Explanation: The length of the diagonal of a square whose sides are of length 10 is 14.1421 Input: S = 24Output: 33.9411 Approach: The given problem can be solved based on the mathematical relation between the length of sides of a square and the length of diagonal of a square as illustrated below: As visible from the above image, the diagonal and the two sides of the square form a right-angled triangle. Therefore, by applying Pythagoras Theorem:(hypotenuse)2 = (base)2 + (perpendicular)2, where D and S are length of the diagonal and the square. Therefore, => D^{2} = S^{2} + S^{2} => D^{2} = 2*S^{2} => D = S \sqrt 2 Therefore, simply calculate the length of the diagonal using the above-derived relation. 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 length of the // diagonal of a square of a given side double findDiagonal(double s) { return sqrt(2) * s; } // Driver Code int main() { double S = 10; cout << findDiagonal(S); return 0; } Java // Java program for the above approach import java.util.*; class GFG{ // Function to find the length of the // diagonal of a square of a given side static double findDiagonal(double s) { return (double)Math.sqrt(2) * s; } // Driver Code public static void main(String[] args) { double S = 10; System.out.print(findDiagonal(S)); } } // This code is contributed by splevel62 Python3 # Python3 program for the above approach import math # Function to find the length of the # diagonal of a square of a given side def findDiagonal(s): return math.sqrt(2) * s # Driver Code if __name__ == "__main__": S = 10 print(findDiagonal(S)) # This code is contributed by chitranayal C# // C# program for the above approach using System; public class GFG { // Function to find the length of the // diagonal of a square of a given side static double findDiagonal(double s) { return (double)Math.Sqrt(2) * s; } // Driver Code public static void Main(String[] args) { double S = 10; Console.Write(findDiagonal(S)); } } // This code is contributed by 29AjayKumar JavaScript <script> // JavaScript program for the above approach // Function to find the length of the // diagonal of a square of a given side function findDiagonal(s) { return Math.sqrt(2) * s; } // Driver Code var S = 10; document.write(findDiagonal(S).toFixed(6)); // This code contributed by shikhasingrajput </script> Output: 14.1421 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to calculate length of diagonal of a square C CoderSaty Follow Improve Article Tags : Misc Mathematical Geometric DSA square-rectangle +1 More Practice Tags : GeometricMathematicalMisc Similar Reads Area of a square from diagonal length Given an number d which is length of diagonal of a square, find its area. Examples: Input : d = 10 Output : Area = 50 Input : d = 12.2 Output : Area = 74.42 Area of a square can be computed as (d * d)/2. Please see below image for details. C++ // C++ Program to find the area of square // when its di 3 min read Program to calculate area of an Circle inscribed in a Square Given the side of a square. The task is to find the area of an inscribed circle in a square.Examples: Input : a = 8 Output : Area of an inscribed circle: 50.24 Input : a = 12.04 Output : Area of an inscribed circle: 113.795 Given a square i.e. all sides of a square are of equal length and all four a 4 min read Program to calculate area of a rhombus whose one side and diagonal are given Given the length of diagonal 'd1' of a rhombus and a side 'a', the task is to find the area of that rhombus. A rhombus is a polygon having 4 equal sides in which both the opposite sides are parallel, and opposite angles are equal. Examples: Input: d = 15, a = 10 Output: 99.21567416492215 Input: d = 3 min read Program for Area Of Square after N-th fold A 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. Given a square of side N and number of folds F . The task is to find the area of sq 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 Like