Â
In this, we are going to write a simple program to identify the type of triangle in C++.
We first get values for the three sides of triangle from user.
check validity of triangle first, using property that sum of two sides must not be less than equal to third side. More about triangle validation in previous post...
We use first check if Pythagoras theorem to satisfied... if so then triangle is a right angles triangle
We check for all three sides being equal then triangle is an Equilateral triangle..
If only two sides are equal and its also right angled then we print Right angled isosceles otherwise only Isosceles triangle...
If its right angled but now sides are equal then its Right angled scalene triangle otherwise only Scalene triangle
The code given below can be used for TURBO C++ Compiler:-
// Check type of triangle using sides
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
double a, b, c;
cout << "Enter sides of triangle\n";
cout << "Enter value of Side a\n";
cin >> a;
cout << "Enter value of Side b\n";
cin >> b;
cout << "Enter value of Side c\n";
cin >> c;
// We will check type of triangle only if triangle is valid
if ((a + b) <= c || (a + c) <= b || (b + c) <= c)
{
cout << "NOT a valid triangle";
}
else
{
bool rightAngled;
// check right angled using pythagoras theorem
if ( (pow(a, 2) == pow(b, 2) + pow(c, 2)) ||
(pow(b, 2) == pow(a, 2) + pow(c, 2)) ||
(pow(c, 2) == pow(b, 2) + pow(a, 2)) )
{
rightAngled = true;
}
// VALID triangle
if (a == b && b == c)
{
cout << "Equilateral triangle";
return 0;
// returned because no need to check right angle or any other type
}
else if ( (a == b && a != c && a != c) ||
(a == c && a != b && c != b) ||
(b == c && b != a && c != a) )
{
// isosceles
if (rightAngled == true){
cout<<"Right angled isosceles triangle";
// returned because no need to check any other type
}
else{
cout<<"Isosceles triangle but NOT right angled";
// returned because no need to check any other type
}
}
else if (a != b && b != c && c != a)
{
// scalene
if (rightAngled == true){
cout<<"Right angled scalene triangle ";
// returned because no need to check any other type
}
else{
cout<<"Scalene triangle but NOT right angled";
// returned because no need to check any other type
}
}
}
getch();
}
The code given below can be used for g++/gcc Compiler:-
// Check type of triangle using sides
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double a, b, c;
cout << "Enter sides of triangle\n";
cout << "Enter value of Side a\n";
cin >> a;
cout << "Enter value of Side b\n";
cin >> b;
cout << "Enter value of Side c\n";
cin >> c;
// We will check type of triangle only if triangle is valid
if ((a + b) <= c || (a + c) <= b || (b + c) <= c)
{
cout << "NOT a valid triangle";
}
else
{
bool rightAngled;
// check right angled using pythagoras theorem
if ( (pow(a, 2) == pow(b, 2) + pow(c, 2)) ||
(pow(b, 2) == pow(a, 2) + pow(c, 2)) ||
(pow(c, 2) == pow(b, 2) + pow(a, 2)) )
{
rightAngled = true;
}
// VALID triangle
if (a == b && b == c)
{
cout << "Equilateral triangle";
return 0;
// returned because no need to check right angle or any other type
}
else if ( (a == b && a != c && a != c) ||
(a == c && a != b && c != b) ||
(b == c && b != a && c != a) )
{
// isosceles
if (rightAngled == true){
cout<<"Right angled isosceles triangle";
// returned because no need to check any other type
}
else{
cout<<"Isosceles triangle but NOT right angled";
// returned because no need to check any other type
}
}
else if (a != b && b != c && c != a)
{
// scalene
if (rightAngled == true){
cout<<"Right angled scalene triangle ";
// returned because no need to check any other type
}
else{
cout<<"Scalene triangle but NOT right angled";
// returned because no need to check any other type
}
}
}
return 0;
}
#ENJOYCODING
Â
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP