Given that all the angles of a quadrilateral are in AP having common differences ‘d’, the task is to find all the angles.
Input: d = 10
Output: 75, 85, 95, 105
Input: d = 20
Output: 60, 80, 100, 120
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++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
int d = 10;
double a;
a = ( double )(360 - (6 * d)) / 4;
cout << a << ", " << a + d << ", " << a + (2 * d)
<< ", " << a + (3 * d) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int d = 10 ;
double a;
a = ( double )( 360 - ( 6 * d)) / 4 ;
System.out.print( a + ", " + (a + d) + ", " + (a + ( 2 * d))
+ ", " + (a + ( 3 * d)));
}
}
|
Python
d = 10
a = 0.0
a = ( 360 - ( 6 * d)) / 4
print (a, "," , a + d, "," , a + 2 * d,
"," , a + 3 * d, sep = ' ' )
|
C#
using System;
class GFG
{
public static void Main ()
{
int d = 10;
double a;
a = ( double )(360 - (6 * d)) / 4;
Console.WriteLine( a + ", " + (a + d) +
", " + (a + (2 * d)) +
", " + (a + (3 * d)));
}
}
|
PHP
<?php
$d = 10;
$a = (360 - (6 * $d )) / 4 ;
echo $a , ", " , $a + $d , ", " ,
$a + (2 * $d ), ", " , $a + (3 * $d );
?>
|
Javascript
<script>
var d = 10;
var a;
a = parseInt((360 - (6 * d)) / 4);
document.write( a + ", " + (a + d) + ", " + (a + (2 * d))
+ ", " + (a + (3 * d)));
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Another 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() {
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;
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;
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);
double cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc);
double cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd);
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() {
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;
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;
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;
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);
double cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc);
double cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd);
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) {
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 ;
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;
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;
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);
double cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc);
double cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd);
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
import math
AX = 0.0
AY = 0.0
BX = 1.0
BY = 2.0
CX = 4.0
CY = 4.0
DX = 2.0
DY = 1.0
ab_x = BX - AX
ab_y = BY - AY
bc_x = CX - BX
bc_y = CY - BY
cd_x = DX - CX
cd_y = DY - CY
dot_product_ab_bc = ab_x * bc_x + ab_y * bc_y
dot_product_bc_cd = bc_x * cd_x + bc_y * cd_y
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)
cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc)
cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd)
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
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;
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;
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;
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);
let cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc);
let cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd);
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() {
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;
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;
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;
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);
double cos_ab_bc = dot_product_ab_bc / (mag_ab * mag_bc);
double cos_bc_cd = dot_product_bc_cd / (mag_bc * mag_cd);
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.