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 intusing namespace std;Ā
// Driver codeint 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 approachd = 10a = 0.0Ā
# according to formula # derived abovea=(360 - (6 * d)) / 4Ā
# print all the anglesprint(a,",", a + d, ",", a + 2 * d,Ā Ā Ā Ā Ā Ā Ā Ā ",", a + 3 * d, sep = ' ')Ā
# This code is contributed # by sahilshelangia |
C#
// C# implementation of the approachusing System;Ā
class GFG {Ā
// Driver codepublic 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 codevar d = 10;var a;Ā
// according to formula derived abovea = parseInt((360 - (6 * d)) / 4);Ā
// print all the anglesdocument.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 Equivalentimport mathĀ
# define the coordinates of the quadrilateral verticesAX = 0.0AY = 0.0BX = 1.0BY = 2.0CX = 4.0CY = 4.0DX = 2.0DY = 1.0Ā
# calculate the vectors AB, BC, and CDab_x = BX - AXab_y = BY - AYbc_x = CX - BXbc_y = CY - BYcd_x = DX - CXcd_y = DY - CYĀ
# calculate the dot products of vectors AB-BC and BC-CDdot_product_ab_bc = ab_x * bc_x + ab_y * bc_ydot_product_bc_cd = bc_x * cd_x + bc_y * cd_yĀ
# calculate the magnitudes of vectors AB, BC, and CDmag_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 formulacos_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 degreesangle_a = math.acos(-cos_ab_bc) * 180 / math.piangle_b = math.acos(cos_ab_bc) * 180 / math.piangle_c = math.acos(cos_bc_cd) * 180 / math.piangle_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 verticesconst 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 CDlet 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-CDlet 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 CDlet 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 formulalet 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 degreeslet 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!
