Open In App

Program to find the angles of a quadrilateral

Last Updated : 27 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given that all the angles of a quadrilateral are in AP having common differences ‘d’, the task is to find all the angles.

Examples:

Input: d = 10
Output: 75, 85, 95, 105

Input: d = 20
Output: 60, 80, 100, 120

Approach:

We know that the angles of the quadrilateral are in AP and having the common difference ‘d’. 
So, if we assume the first angle to be ‘a’ then the other angles can be calculated as, 
‘a+d’, ‘a+2d’ and ‘a+3d’ 
And, from the properties of quadrilaterals, the sum of all the angles of a quadrilateral is 360. So, 
(a) + (a + d) + (a + 2*d) + (a + 3*d) = 360 
4*a + 6*d = 360 
a = (360 – (6*d)) / 4 
where ‘a’ is the angle assumed in the beginning and ‘d’ is the common difference.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
 
// Driver code
int main()
{
    int d = 10;
    double a;
 
    // according to formula derived above
    a = (double)(360 - (6 * d)) / 4;
 
    // print all the angles
    cout << a << ", " << a + d << ", " << a + (2 * d)
         << ", " << a + (3 * d) << endl;
    return 0;
}


Java




// java implementation of the approach
 
import java.io.*;
 
class GFG {
  
// Driver code
 
    public static void main (String[] args) {
            int d = 10;
    double a;
 
    // according to formula derived above
    a = (double)(360 - (6 * d)) / 4;
 
    // print all the angles
    System.out.print( a + ", " + (a + d) + ", " + (a + (2 * d))
        + ", " + (a + (3 * d)));
    }
}
//This code is contributed
//by  inder_verma


Python




# Python implementation
# of the approach
d = 10
a = 0.0
 
# according to formula
# derived above
a=(360 - (6 * d)) / 4
 
# print all the angles
print(a,",", a + d, ",", a + 2 * d,
        ",", a + 3 * d, sep = ' ')
 
# This code is contributed
# by sahilshelangia


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Driver code
public static void Main ()
{
    int d = 10;
    double a;
     
    // according to formula derived above
    a = (double)(360 - (6 * d)) / 4;
     
    // print all the angles
    Console.WriteLine( a + ", " + (a + d) +
                           ", " + (a + (2 * d)) +
                           ", " + (a + (3 * d)));
}
}
 
// This code is contributed
// by anuj_67


PHP




<?php
// PHP implementation of the approach
 
// Driver code
$d = 10;
 
// according to formula
// derived above
$a = (360 - (6 * $d)) / 4 ;
 
// print all the angles
echo $a, ", ", $a + $d , ", ",
     $a + (2 * $d), ", ", $a + (3 * $d);
 
// This code is contributed
// by ANKITRAI1
?>


Javascript




<script>
 
// Javascript implementation of the approach
 
// Driver code
var d = 10;
var a;
 
// according to formula derived above
a = parseInt((360 - (6 * d)) / 4);
 
// print all the angles
document.write( a + ", " + (a + d) + ", " + (a + (2 * d))
    + ", " + (a + (3 * d)));
 
 
</script>


Output

75, 85, 95, 105

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

Another approach:

Approach:

1. Define constants for the coordinates of the quadrilateral vertices.
2. Calculate the vectors AB, BC, and CD using the coordinates.
3. Calculate the dot products of vectors AB-BC and BC-CD.
4. Calculate the magnitudes of vectors AB, BC, and CD.
5. Calculate the cosine of angles between vectors AB-BC and BC-CD using dot product formula.
6. Calculate the angles using inverse cosine function (arccos) and convert to degrees.
7. Print the angles of the quadrilateral.

C




#include <stdio.h>
#include <math.h>
 
#define AX 0.0
#define AY 0.0
#define BX 1.0
#define BY 2.0
#define CX 4.0
#define CY 4.0
#define DX 2.0
#define DY 1.0
 
int main() {
    // calculate the vectors AB, BC, and CD
    double ab_x = BX - AX;
    double ab_y = BY - AY;
    double bc_x = CX - BX;
    double bc_y = CY - BY;
    double cd_x = DX - CX;
    double cd_y = DY - CY;
 
    // calculate the dot products of vectors AB-BC and BC-CD
    double dot_product_ab_bc = ab_x * bc_x + ab_y * bc_y;
    double dot_product_bc_cd = bc_x * cd_x + bc_y * cd_y;
 
    // calculate the magnitudes of vectors AB, BC, and CD
    double mag_ab = sqrt(ab_x * ab_x + ab_y * ab_y);
    double mag_bc = sqrt(bc_x * bc_x + bc_y * bc_y);
    double mag_cd = sqrt(cd_x * cd_x + cd_y * cd_y);
 
    // calculate the cosine of angles between vectors AB-BC and BC-CD using dot product formula
    double cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc);
    double cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd);
 
    // calculate the angles using inverse cosine function (arccos) and convert to degrees
    double angle_a = acos(-cos_ab_bc) * 180 ;
    double angle_b = acos(cos_ab_bc) * 180 ;
    double angle_c = acos(cos_bc_cd) * 180 ;
    double angle_d = acos(-cos_bc_cd) * 180 ;
 
    printf("The angles of the quadrilateral are A=%.2f, B=%.2f, C=%.2f, and D=%.2f degrees.\n", angle_a, angle_b, angle_c, angle_d);
    return 0;
}


C++




#include <iostream>
#include <math.h>
 
using namespace std;
 
int main() {
    // defining coordinates of the quadrilateral's vertices
    const double AX = 0.0;
    const double AY = 0.0;
    const double BX = 1.0;
    const double BY = 2.0;
    const double CX = 4.0;
    const double CY = 4.0;
    const double DX = 2.0;
    const double DY = 1.0;
 
    // calculating vectors AB, BC, and CD
    double ab_x = BX - AX;
    double ab_y = BY - AY;
    double bc_x = CX - BX;
    double bc_y = CY - BY;
    double cd_x = DX - CX;
    double cd_y = DY - CY;
 
    // calculate the dot products of vectors AB-BC and BC-CD
    double dot_product_ab_bc = ab_x * bc_x + ab_y * bc_y;
    double dot_product_bc_cd = bc_x * cd_x + bc_y * cd_y;
 
    // calculate the magnitudes of vectors AB, BC, and CD
    double mag_ab = sqrt(ab_x * ab_x + ab_y * ab_y);
    double mag_bc = sqrt(bc_x * bc_x + bc_y * bc_y);
    double mag_cd = sqrt(cd_x * cd_x + cd_y * cd_y);
 
    // calculate the cosine of angles between vectors AB-BC and BC-CD using dot product formula
    double cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc);
    double cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd);
 
    // calculate the angles using inverse cosine function (arccos) and convert to degrees
    double angle_a = acos(-cos_ab_bc) * 180 / M_PI;
    double angle_b = acos(cos_ab_bc) * 180 / M_PI;
    double angle_c = acos(cos_bc_cd) * 180 / M_PI;
    double angle_d = acos(-cos_bc_cd) * 180 / M_PI;
 
    cout << "The angles of the quadrilateral are A=" << fixed << angle_a << ", B=" << fixed << angle_b << ", C=" << fixed << angle_c << ", and D=" << fixed << angle_d << " degrees." << endl;
 
    return 0;
}


Java




import java.lang.Math;
 
public class Main {
    public static void main(String[] args) {
        // define the coordinates of the quadrilateral vertices
        final double AX = 0.0;
        final double AY = 0.0;
        final double BX = 1.0;
        final double BY = 2.0;
        final double CX = 4.0;
        final double CY = 4.0;
        final double DX = 2.0;
        final double DY = 1.0;
 
        // calculate the vectors AB, BC, and CD
        double ab_x = BX - AX;
        double ab_y = BY - AY;
        double bc_x = CX - BX;
        double bc_y = CY - BY;
        double cd_x = DX - CX;
        double cd_y = DY - CY;
 
        // calculate the dot products of vectors AB-BC and BC-CD
        double dot_product_ab_bc = ab_x * bc_x + ab_y * bc_y;
        double dot_product_bc_cd = bc_x * cd_x + bc_y * cd_y;
 
        // calculate the magnitudes of vectors AB, BC, and CD
        double mag_ab = Math.sqrt(ab_x * ab_x + ab_y * ab_y);
        double mag_bc = Math.sqrt(bc_x * bc_x + bc_y * bc_y);
        double mag_cd = Math.sqrt(cd_x * cd_x + cd_y * cd_y);
 
        // calculate the cosine of angles between vectors AB-BC and BC-CD using dot product formula
        double cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc);
        double cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd);
 
        // calculate the angles using inverse cosine function (arccos) and convert to degrees
        double angle_a = Math.acos(-cos_ab_bc) * 180 / Math.PI;
        double angle_b = Math.acos(cos_ab_bc) * 180 / Math.PI;
        double angle_c = Math.acos(cos_bc_cd) * 180 / Math.PI;
        double angle_d = Math.acos(-cos_bc_cd) * 180 / Math.PI;
 
        System.out.printf("The angles of the quadrilateral are A=%.2f, B=%.2f, C=%.2f, and D=%.2f degrees.%n", angle_a, angle_b, angle_c, angle_d);
    }
}


Python3




# Python Equivalent
import math
 
# define the coordinates of the quadrilateral vertices
AX = 0.0
AY = 0.0
BX = 1.0
BY = 2.0
CX = 4.0
CY = 4.0
DX = 2.0
DY = 1.0
 
# calculate the vectors AB, BC, and CD
ab_x = BX - AX
ab_y = BY - AY
bc_x = CX - BX
bc_y = CY - BY
cd_x = DX - CX
cd_y = DY - CY
 
# calculate the dot products of vectors AB-BC and BC-CD
dot_product_ab_bc = ab_x * bc_x + ab_y * bc_y
dot_product_bc_cd = bc_x * cd_x + bc_y * cd_y
 
# calculate the magnitudes of vectors AB, BC, and CD
mag_ab = math.sqrt(ab_x * ab_x + ab_y * ab_y)
mag_bc = math.sqrt(bc_x * bc_x + bc_y * bc_y)
mag_cd = math.sqrt(cd_x * cd_x + cd_y * cd_y)
 
# calculate the cosine of angles between vectors AB-BC and BC-CD using dot product formula
cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc)
cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd)
 
# calculate the angles using inverse cosine function (arccos) and convert to degrees
angle_a = math.acos(-cos_ab_bc) * 180 / math.pi
angle_b = math.acos(cos_ab_bc) * 180 / math.pi
angle_c = math.acos(cos_bc_cd) * 180 / math.pi
angle_d = math.acos(-cos_bc_cd) * 180 / math.pi
 
print(f"The angles of the quadrilateral are A={angle_a:.2f}, B={angle_b:.2f}, C={angle_c:.2f}, and D={angle_d:.2f} degrees.")


Javascript




// define the coordinates of the quadrilateral vertices
const AX = 0.0;
const AY = 0.0;
const BX = 1.0;
const BY = 2.0;
const CX = 4.0;
const CY = 4.0;
const DX = 2.0;
const DY = 1.0;
 
// calculate the vectors AB, BC, and CD
let ab_x = BX - AX;
let ab_y = BY - AY;
let bc_x = CX - BX;
let bc_y = CY - BY;
let cd_x = DX - CX;
let cd_y = DY - CY;
 
// calculate the dot products of vectors AB-BC and BC-CD
let dot_product_ab_bc = ab_x * bc_x + ab_y * bc_y;
let dot_product_bc_cd = bc_x * cd_x + bc_y * cd_y;
 
// calculate the magnitudes of vectors AB, BC, and CD
let mag_ab = Math.sqrt(ab_x * ab_x + ab_y * ab_y);
let mag_bc = Math.sqrt(bc_x * bc_x + bc_y * bc_y);
let mag_cd = Math.sqrt(cd_x * cd_x + cd_y * cd_y);
 
// calculate the cosine of angles between vectors AB-BC and BC-CD using dot product formula
let cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc);
let cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd);
 
// calculate the angles using inverse cosine function (arccos) and convert to degrees
let angle_a = Math.acos(-cos_ab_bc) * 180 / Math.PI;
let angle_b = Math.acos(cos_ab_bc) * 180 / Math.PI;
let angle_c = Math.acos(cos_bc_cd) * 180 / Math.PI;
let angle_d = Math.acos(-cos_bc_cd) * 180 / Math.PI;
 
console.log(`The angles of the quadrilateral are A=${angle_a.toFixed(2)}, B=${angle_b.toFixed(2)}, C=${angle_c.toFixed(2)}, and D=${angle_d.toFixed(2)} degrees.`);


C#




using System;
 
class MainClass {
    public static void Main() {
        // define the coordinates of the quadrilateral vertices
        const double AX = 0.0;
        const double AY = 0.0;
        const double BX = 1.0;
        const double BY = 2.0;
        const double CX = 4.0;
        const double CY = 4.0;
        const double DX = 2.0;
        const double DY = 1.0;
 
        // calculate the vectors AB, BC, and CD
        double ab_x = BX - AX;
        double ab_y = BY - AY;
        double bc_x = CX - BX;
        double bc_y = CY - BY;
        double cd_x = DX - CX;
        double cd_y = DY - CY;
 
        // calculate the dot products of vectors AB-BC and BC-CD
        double dot_product_ab_bc = ab_x * bc_x + ab_y * bc_y;
        double dot_product_bc_cd = bc_x * cd_x + bc_y * cd_y;
 
        // calculate the magnitudes of vectors AB, BC, and CD
        double mag_ab = Math.Sqrt(ab_x * ab_x + ab_y * ab_y);
        double mag_bc = Math.Sqrt(bc_x * bc_x + bc_y * bc_y);
        double mag_cd = Math.Sqrt(cd_x * cd_x + cd_y * cd_y);
 
        // calculate the cosine of angles between vectors AB-BC and BC-CD using dot product formula
        double cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc);
        double cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd);
 
        // calculate the angles using inverse cosine function (arccos) and convert to degrees
        double angle_a = Math.Acos(-cos_ab_bc) * 180 / Math.PI;
        double angle_b = Math.Acos(cos_ab_bc) * 180 / Math.PI;
        double angle_c = Math.Acos(cos_bc_cd) * 180 / Math.PI;
        double angle_d = Math.Acos(-cos_bc_cd) * 180 / Math.PI;
 
        Console.WriteLine("The angles of the quadrilateral are A={0:f2}, B={1:f2}, C={2:f2}, and D={3:f2} degrees.", angle_a, angle_b, angle_c, angle_d);
    }
}


Output

The angles of the quadrilateral are A=472.04, B=93.45, C=494.42, and D=71.06 degrees.

Time Complexity:
The time complexity of this program is O(1) because it always performs the same number of operations regardless of the size of the input.

Space Complexity:
The space complexity of this program is O(1) because it uses a fixed amount of memory to store the constants, variables, and calculations.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads