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> |
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); Ā Ā Ā Ā } } |
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!