Length of diagonals of a Rhombus using length of Side and vertex Angle

Given two integers A and theta, denoting the length of a side of a rhombus and the vertex angle respectively, the task is to find the length of the diagonals of the rhombus.
Examples:
Input: A = 10, theta = 30Â
Output: 19.32 5.18Input: A = 6, theta = 45Â
Output: 11.09 4.59
Approach:Â
The problem can be solved using the law of cosines. Using the law of cosines on triangles formed by the diagonals and sides of the rhombus gives the following relation to calculate the length of diagonals:
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 rhombus using// length of sides and vertex angledouble Length_Diagonals(int a, double theta){    double p = a * sqrt(2 + (2 * cos(           theta * (3.141 / 180))));    double q = a * sqrt(2 - (2 * cos(           theta * (3.141 / 180))));                cout << fixed << setprecision(2) << p         << " " << q; }Â
// Driver Code int main() {     int a = 6;    int theta = 45;       // Function Call     Length_Diagonals(a, theta);        return 0; } Â
// This code is contributed by Virusbuddah_ |
Java
// Java program to implement// the above approachclass GFG{Â
// Function to calculate the length// of diagonals of a rhombus using// length of sides and vertex anglestatic double[] Length_Diagonals(int a, double theta){Â Â Â Â double p = a * Math.sqrt(2 + (2 *Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Math.cos(theta * (Math.PI / 180))));Â
    double q = a * Math.sqrt(2 - (2 *                   Math.cos(theta * (Math.PI / 180))));Â
    return new double[]{ p, q };}Â
// Driver Codepublic static void main(String[] args) {Â Â Â Â int A = 6;Â Â Â Â double theta = 45;Â Â Â Â Â Â Â Â Â double[] ans = Length_Diagonals(A, theta);Â
    System.out.printf("%.2f" + " " + "%.2f",                      ans[0], ans[1]);}}Â
// This code is contributed by Princi Singh |
Python3
# Python Program to implement# the above approachimport mathÂ
# Function to calculate the length# of diagonals of a rhombus using# length of sides and vertex angledef Length_Diagonals(a, theta):Â
    p = a * math.sqrt(2 + (2 * \            math.cos(math.radians(theta))))                 q = a * math.sqrt(2 - (2 * \            math.cos(math.radians(theta))))Â
    return [p, q]Â
Â
# Driver CodeA = 6theta = 45Â
ans = Length_Diagonals(A, theta)Â
print(round(ans[0], 2), round(ans[1], 2)) |
C#
// C# program to implement// the above approachusing System;class GFG{Â
// Function to calculate the length// of diagonals of a rhombus using// length of sides and vertex anglestatic double[] Length_Diagonals(int a, double theta){Â Â Â Â double p = a * Math.Sqrt(2 + (2 *Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Math.Cos(theta * (Math.PI / 180))));Â
    double q = a * Math.Sqrt(2 - (2 *                   Math.Cos(theta * (Math.PI / 180))));Â
    return new double[]{ p, q };}Â
// Driver Codepublic static void Main(String[] args) {Â Â Â Â int A = 6;Â Â Â Â double theta = 45;Â Â Â Â Â Â Â Â Â double[] ans = Length_Diagonals(A, theta);Â
    Console.Write("{0:F2}" + " " + "{1:F2}",                            ans[0], ans[1]);}}Â
// This code is contributed by gauravrajput1 |
Javascript
<script>Â
// JavaScript program for the above approachÂ
// Function to calculate the length// of diagonals of a rhombus using// length of sides and vertex anglefunction Length_Diagonals(a, theta){    let p = a * Math.sqrt(2 + (2 *                Math.cos(theta * (Math.PI / 180))));       let q = a * Math.sqrt(2 - (2 *                Math.cos(theta * (Math.PI / 180))));       return [ p, q ];}     // Driver Code           let A = 6;    let theta = 45;           let ans = Length_Diagonals(A, theta);       document.write(ans[0].toFixed(2) + " "    + ans[1].toFixed(2));      </script> |
Output:Â
11.09 4.59
Â
Time Complexity: O(1)Â
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!




