VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-calculate-the-length-of-the-diagonal-in-a-rectangle/

⇱ Program to calculate the length of the diagonal in a rectangle - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to calculate the length of the diagonal in a rectangle

Last Updated : 18 Jan, 2024

Write a program to calculate the length of the diagonal in a rectangle with the given length and breadth.

Examples:

Input: Length = 5, Width = 3
Output: Diagonal Length = 5.83095

Input: Length = 8, Width = 6
Output: 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:


Output
Diagonal Length: 5.83095

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

Comment
Article Tags:
Article Tags: