- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find other two sides of a right angle triangle in C++
In this problem, we are given an integer a denoting one side of a right angle triangle. We need to check whether it is possible to have a right angle triangle with side a. If it is possible, then find the other two sides of a right angle triangle.
Let’s take an example to understand the problem,
Input
a = 5
Output
Sides : 5, 12, 13
Explanation
The sides of right angle are found as 52 + 122 = 132
Solution Approach
A simple solution to the problem is using pythagoras theorem. We know that the sides of a right angled triangle follow pythagoras theorem, which is
a2 + b2 = c2
Where a and b are sides of the triangle and c is the hypotenuse of the triangle.
Using this, we will calculate values of b and c using a.
Case 1
If a is even, c = (a2 + 4) + 1 b = (a2 + 4) - 1
Case 2
If a is odd, c = (a2 + 1)/ 2 c = (a2 - 1)/ 2
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> #include <cmath> using namespace std; #define PI 3.1415926535 void printOtherSides(int n) { int b,c; if (n & 1) { if (n == 1) cout << -1 << endl; else{ b = (n*n-1)/2; c = (n*n+1)/2; } } else { if (n == 2) cout << -1 << endl; else{ b = n*n/4-1; c = n*n/4+1; } } cout<<"Sides : a = "<<n<<", b = "<<b<<", c = "<<c<<endl; } int main() { int a = 5; printOtherSides(a); return 0; }
Output
Sides : a = 5, b = 12, c = 13
- Related Articles
- Find other two sides and angles of a right angle triangle in C++
- Find the hypotenuse of a right angled triangle with given two sides in C++
- Prove that in a right angle triangle, the square of the hypotenuse is equal the sum of squares of the other two sides.
- The hypotenuse of a right angled triangle is ( 5 mathrm{~cm} ) and the other two sides differ by ( 1 mathrm{~cm} ). Find the other two sided of the triangle.
- If one angle of a triangle is equal to the sum of the other two, show that the triangle is a right triangle.
- The hypotenuse of a right triangle is 25 cm. The difference between the lengths of the other two sides of the triangle is 5 cm. Find the lengths of these sides.
- Which of the following statements are true (T) and which are false (F):If any two sides of a right triangle are respectively equal to two sides of other right triangle, then the two triangles are congruent.
- Find the area of the right triangle whose sides containing the right angle are 8 cm and 15 cm."
- In a right triangle ( A B C ), right angled at ( C ), if ( angle B=60^{circ} ) and ( A B=15 ) units. Find the remaining angles and sides.
- Is it true to say that if in two triangles, an angle of one triangle is equal to an angle of another triangle and two sides of one triangle are proportional to the two sides of the other triangle, then the triangles are similar? Give reasons for your answer.
- The altitude of a right triangle is 7 cm less than its base. If the hypotenuse is 13 cm, find the other two sides.
- Draw a right triangle ( A B C ) in which ( A C=A B=4.5 mathrm{~cm} ) and ( angle A=90^{circ} . ) Draw a triangle similar to ( triangle A B C ) with its sides equal to ( (5 / 4) ) th of the corresponding sides of ( triangle A B C ).
- In a $triangle ABC$ right angled at B, $angle A = angle C$. Find the values of$sin A cos C + cos A sin C$
- A right angled triangle whose sides are ( 3 mathrm{~cm}, 4 mathrm{~cm} ) and ( 5 mathrm{~cm} ) is revolved about the sides containing the right angle in two ways. Find the difference in volumes of the two cones so formed. Also, find their curved surfaces.
- $ABC$ is a right angled triangle in which $angle A = 90^o$ and $AB = AC$. Find $angle B$ and $angle C$.

Advertisements