Length of Diagonals of a Cyclic Quadrilateral using the length of Sides.

Given integers A, B, C, and D, denoting the length of sides of a Cyclic Quadrilateral, the task is to find the length of diagonals of a cyclic quadrilateral.
Examples:
Input: A = 10, B = 15, C = 20, D = 25Â
Output: 22.06 26.07Input: A = 10, B = 30, C =50, D = 20Â
Output: 37.93 29.0Â
Â
Approach: The length of diagonals can be calculated using the following equations:Â Â
[Tex]Diagonal (q)=\sqrt{\frac{(ac+bd)(ab+cd)}{ad+bc}}Â Â Â Â Â Â Â Â Â Â [/Tex]
Below is the implementation of the above approach:Â
C++
// C++ Program to implement// the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to calculate the length of// diagonals of a cyclic quadrilateralvector<float> Diagonals(int a, int b, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int c, int d){Â Â Â Â vector<float> ans;Â Â Â Â ans.push_back(sqrt(((a * c) + (b * d)) * Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * d) + (b * c)) / Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * b) + (c * d))));Â Â Â Â ans.push_back(sqrt(((a * c) + (b * d)) * Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * b) + (c * d)) / Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * d) + (b * c))));Â Â Â Â return ans;}Â
// Driver Codeint main(){Â Â Â Â int A = 10;Â Â Â Â int B = 15;Â Â Â Â int C = 20;Â Â Â Â int D = 25;Â
    // Function Call    vector<float> ans = Diagonals(A, B, C, D);Â
    // Print the final answer    printf("%.2f %.2f",            (ans[0]) + .01,             ans[1] + .01);}Â
// This code is contributed by Amit Katiyar |
Java
// Java Program to implement// the above approachimport java.util.*;class GFG{Â
// Function to calculate the length of// diagonals of a cyclic quadrilateralstatic Vector<Float> Diagonals(int a, int b, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int c, int d){Â Â Â Â Vector<Float> ans = new Vector<Float>();Â Â Â Â ans.add((float) Math.sqrt(((a * c) + (b * d)) * Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * d) + (b * c)) / Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * b) + (c * d))));Â Â Â Â ans.add((float) Math.sqrt(((a * c) + (b * d)) * Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * b) + (c * d)) / Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * d) + (b * c))));Â Â Â Â return ans;}Â
// Driver Codepublic static void main(String[] args){Â Â Â Â int A = 10;Â Â Â Â int B = 15;Â Â Â Â int C = 20;Â Â Â Â int D = 25;Â
    // Function Call    Vector<Float> ans = Diagonals(A, B,                                   C, D);Â
    // Print the final answer    System.out.printf("%.2f %.2f",                       (ans.get(0)) + .01,                        ans.get(1) + .01);}}Â
// This code is contributed by 29AjayKumar |
Python3
# Python3 program to implement# the above approachÂ
import mathÂ
# Function to calculate the length of# diagonals of a cyclic quadrilateraldef Diagonals(a, b, c, d):Â
    p = math.sqrt(((a * c)+(b * d))*((a * d)+(b * c))                  / ((a * b)+(c * d)))    q = math.sqrt(((a * c)+(b * d))*((a * b)+(c * d))                  / ((a * d)+(b * c)))Â
    return [p, q]Â
Â
# Driver CodeA = 10B = 15C = 20D = 25Â
# Function Callans = Diagonals(A, B, C, D)Â
# Print the final answerprint(round(ans[0], 2), round(ans[1], 2)) |
C#
// C# Program to implement// the above approachusing System;using System.Collections.Generic;class GFG{Â
// Function to calculate the length of// diagonals of a cyclic quadrilateralstatic List<float> Diagonals(int a, int b, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int c, int d){Â Â List<float> ans = new List<float>();Â Â ans.Add((float) Math.Sqrt(((a * c) + (b * d)) * Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * d) + (b * c)) / Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * b) + (c * d))));Â Â ans.Add((float) Math.Sqrt(((a * c) + (b * d)) * Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * b) + (c * d)) / Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((a * d) + (b * c))));Â Â return ans;}Â
// Driver Codepublic static void Main(String[] args){Â Â int A = 10;Â Â int B = 15;Â Â int C = 20;Â Â int D = 25;Â
  // Function Call  List<float> ans = Diagonals(A, B,                               C, D);Â
  // Print the readonly answer  Console.Write("{0:F2} {1:F2}",                  (ans[0]) + .01,                   ans[1] + .01);}} Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// Javascript Program to implement// the above approachÂ
// Function to calculate the length of// diagonals of a cyclic quadrilateralfunction Diagonals(a, b, c, d){Â
    var p = parseFloat(            Math.sqrt(((a * c) + (b * d)) *                       ((a * d) + (b * c)) /                       ((a * b) + (c * d))));    var q = parseFloat(            Math.sqrt(((a * c) + (b * d)) *                       ((a * b) + (c * d)) /                       ((a * d) + (b * c))));Â
    return [p, q];}Â
// Driver Codevar A = 10;var B = 15;var C = 20;var D = 25;Â
// Function Callvar ans = Diagonals(A, B, C, D)Â
// Print the final answerdocument.write(ans[0].toFixed(2) + " ", Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ans[1].toFixed(2));Â
// This code is contributed by kirtiÂ
</script> |
Output:Â
22.06 26.07
Â
Time Complexity: O(log n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




