Q:

Program to find the type of triangle from the given coordinates

0

In this article, we will see the programs to find the type of triangle from the given coordinates in different programming languages.

Before jumping directly to the programs, let's first see a brief description of the triangle.

Triangle

A triangle is a polygon that has three vertices and three edges. The sum of three interior angles of a triangle is 180 degrees. Triangles are of three types that are listed as follows -

  • Equilateral triangle
  • Isosceles triangle
  • Scalene triangle

Let's see a brief description of types of triangles.

Equilateral triangle

Each angle of an equilateral triangle is of 60 degrees. It has three equal-length sides and three equal angles. It has three lines of symmetry.

Isosceles triangle

An isosceles triangle has one line of symmetry. It has two equal sides and two equal angles.

Scalene triangle

A scalene triangle has no line of symmetry. It has no equal sides and no equal angles.

Now, let's see the programs to find the type of triangle from the given coordinates.

Programs to find the type of triangle from the given coordinates

The approach that we are using in programs is given as follows -

  • The programs will be made by comparing the given length of sides of the triangle. This classification is simple because if all sides are equivalent triangle will be equilateral, if two sides are equal triangle will be isosceles, if no sides are equal triangle will be scalene.
  • And after comparing the sides, there will be a comparison between angles. Here, we require the square of sides.
  • If the sum of square of the two sides is equal to the square of the third side, the triangle will be a right-angle triangle.
  • If the sum of squares of the smaller sides is less than the square of the largest side, the triangle will be an obtuse angle triangle.
  • If the sum of the squares of the two sides of a triangle is greater than the square of the largest side, the triangle will be an acute angle triangle.

So, basically, we will use the sides of a triangle to find its type and angle.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program: Write a program to find the type of triangle from the given coordinates in C++.

  

#include <iostream>  
using namespace std;  
  
string typeTriangle(int a, int b, int c) // function to calculate the type of triangle  
{  
    if (a == b && b == c)  
        return "Equilateral";  
    else if (a == b || b == c || a == c)  
        return "Isosceles";  
    else  
        return "Scalene";  
}  
string angle(int a, int b, int c) // function to calculate the angle of triangle  
{  
int x = a * a;  
int y = b * b;  
int z = c * c;  
if(a>b && a>c) // here, the largest side is 'a'  
{  
    if(x == y + z)  
    return "right-angled";  
    else if(x < y + z)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
      
}  
  
else if(b>a && b>c) //here, the largest side is 'b'  
{  
   if(y == x + z)  
    return "right-angled";  
    else if(y < x + z)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
}  
else //here, the largest side is 'c'  
{  
      if(z == x + y)  
    return "right-angled";  
    else if(z < x + y)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
}  
}  
string triangle(int a, int b, int c)  
{  
    cout << "Triangle is " + angle(a, b, c) + " and " + typeTriangle(a, b, c) << endl;  
}  
int main()  
{  
    int a, b, c;  
    cout<<"Enter the sides of triangle: ";  
    cin>>a>>b>>c;  
    if(a == 0 || b == 0 || c == 0) // to check whether length of any side is 0 or not  
      cout<<"Triangle cannot be formed";  
            else  
      triangle(a, b, c);  
      
    return 0;  
}

Output:

 

Program: Write a program to find the type of triangle from the given coordinates in C#.

using System;  
class JTP  
{  
static string typeTriangle(int a, int b, int c) //function to find type of triangle  
{  
    if (a == b && b == c)  
        return "Equilateral";  
    else if (a == b || b == c || a == c)  
        return "Isosceles";  
    else  
        return "Scalene";  
}  
static string angle(int a, int b, int c) //function to find angle  
{  
int x = a * a;  
int y = b * b;  
int z = c * c;  
if(a>b && a>c) // when largest side is 'a'  
{  
    if(x == y + z)  
    return "right-angled";  
    else if(x < y + z)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
      
}  
else if(b>a && b>c) // when largest side is 'b'  
{  
   if(y == x + z)  
    return "right-angled";  
    else if(y < x + z)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
}  
else // when largest side is 'c'  
{  
    if(z == x + y)  
    return "right-angled";  
    else if(z < x + y)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
  
}  
}  
static void triangle(int a, int b, int c)  
{  
    Console.WriteLine("Triangle is " + angle(a, b, c) + " and " + typeTriangle(a, b, c));  
}  
static public void Main()  
{  
    int a, b, c;  
    Console.WriteLine("Enter the sides of triangle: ");  
    a = Convert.ToInt32(Console.ReadLine());  
    b = Convert.ToInt32(Console.ReadLine());  
    c = Convert.ToInt32(Console.ReadLine());  
    if(a == 0 || b == 0 || c == 0) // to check whether length of any side is 0 or not  
      Console.WriteLine("Triangle cannot be formed");  
    else  
      triangle(a, b, c);  
}  
}  

Output:

After the execution of the above code, and entering the input, the output will be -

 

Program: Write a program to find the type of triangle from the given coordinates in Java.

import java.util.*;  
class Triangle  
{     
static String triangleType(int a, int b, int c) //function to find type of triangle  
{  
    if (a == b && b == c)  
        return "Equilateral";  
    else if (a == b || b == c || a == c)  
        return "Isosceles";  
    else  
        return "Scalene";  
  
}  
static String angleType(int a, int b, int c) //function to find angle  
{  
int x = a * a;  
int y = b * b;  
int z = c * c;  
if(a>b && a>c) // when largest side is 'a'  
{  
    if(x == y + z)  
    return "right-angled";  
    else if(x < y + z)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
      
}  
else if(b>a && b>c) // when largest side is 'b'  
{  
   if(y == x + z)  
    return "right-angled";  
    else if(y < x + z)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
}  
else // when largest side is 'c'  
{  
    if(z == x + y)  
    return "right-angled";  
    else if(z < x + y)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
  
}  
}  
static void triangle(int a, int b, int c)  
{  
System.out.println("Triangle is " + triangleType(a, b, c) + " and " +  angleType(a, b, c));  
}  
                  
public static void main(String[] args)  
{  
    int a, b, c;  
    System.out.println("Enter the sides of triangle: ");  
    Scanner sc = new Scanner(System.in);  
    a = sc.nextInt();  
    b = sc.nextInt();  
    c = sc.nextInt();  
    if(a == 0 || b == 0 || c == 0) // to check whether length of any side is 0 or not  
      System.out.println("Triangle cannot be formed");  
    else  
      triangle(a, b, c);  
}  
}  

Output

 

Program: Write a program to find the type of triangle from the given coordinates in JavaScript.

<html>  
<body>  
<script>  
function triangleType(a, b, c)  
{  
  
    if (a == b && b == c)  
        return "Equilateral";  
    else if (a == b || b == c || a == c)  
        return "Isosceles";  
    else  
        return "Scalene";  
  
}  
function angleType(a, b, c)  
{  
var x = a * a;  
var y = b * b;  
var z = c * c;  
if(a>b && a>c) // when largest side is 'a'  
{  
    if(x == y + z)  
    return "right-angled";  
    else if(x < y + z)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
      
}  
else if(b>a && b>c) // when largest side is 'b'  
{  
   if(y == x + z)  
    return "right-angled";  
    else if(y < x + z)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
}  
else // when largest side is 'c'  
{  
    if(z == x + y)  
    return "right-angled";  
    else if(z < x + y)  
    return "acute-angled";  
    else  
    return "obtuse-angled";  
  
}  
  
}  
function triangle(a, b, c)  
{  
document.write("a = " + a + ", b = " + b + ", c = " + c);  
document.write("<br/>Triangle is " + triangleType(a, b, c) + " and " +  angleType(a, b, c));  
}  
    let a = prompt("Enter the first side: ");  
    let b = prompt("Enter the second side: ");  
    let c = prompt("Enter the third side: ");  
    if(a == 0 || b == 0 || c == 0) // to check whether length of any side is 0 or not  
      document.write("Triangle cannot be formed");  
    else  
      triangle(a, b, c);  
  
</script>  
</body>  
</html>  

Output

After the execution of above code, the output will be -

Similarly, we have to enter values for two more sides of triangle, after entering the values, the output will be -

So, that's all about the article. Here, we have discussed the programs to find the type of a triangle from the given coordinates in C++C#Java, and JavaScript. Hope you find the article helpful and informative.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions