Program to calculate the length of the diagonal in a rectangle Last Updated : 18 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Write a program to calculate the length of the diagonal in a rectangle with the given length and breadth. Examples: Input: Length = 5, Width = 3Output: Diagonal Length = 5.83095 Input: Length = 8, Width = 6Output: Diagonal Length = 10 Approach: To solve the problem, follow the below idea: The diagonal of a rectangle forms a right-angled triangle with the length and width of the rectangle. We can use the Pythagoras theorem to calculate the length of the diagonal. So, we can use this formula Diagonal2 = Length2 + Breadth2 to calculate the length of the diagonal. Step-by-step algorithm: Square the length and width.Sum the squares.Take the square root of the sum.Below is the implementation of the algorithm: C++ #include <cmath> #include <iostream> using namespace std; int main() { double L = 5, W = 3; double diagonal = sqrt(L * L + W * W); cout << "Diagonal Length: " << diagonal << endl; return 0; } C #include <math.h> #include <stdio.h> int main() { double L = 5, W = 3; double diagonal = sqrt(L * L + W * W); printf("Diagonal Length: %lf\n", diagonal); return 0; } Java public class DiagonalLength { public static void main(String[] args) { double L = 5, W = 3; double diagonal = Math.sqrt(Math.pow(L, 2) + Math.pow(W, 2)); System.out.println("Diagonal Length: " + diagonal); } } Python3 import math L, W = 5, 3 diagonal = math.sqrt(L**2 + W**2) print(f"Diagonal Length: {diagonal}") C# using System; class Program { static void Main() { double L = 5, W = 3; double diagonal = Math.Sqrt(Math.Pow(L, 2) + Math.Pow(W, 2)); Console.WriteLine("Diagonal Length: " + diagonal); } } JavaScript let L = 5, W = 3; let diagonal = Math.sqrt(L ** 2 + W ** 2); console.log("Diagonal Length: " + diagonal); OutputDiagonal Length: 5.83095 Time Complexity: O(1)Auxiliary Space: O(1) Create Quiz Comment C code_r Follow 0 Improve C code_r Follow 0 Improve Article Tags : DSA Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 3 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like