Program to Check Type of Triangle

Program to Check Type of Triangle

  • Write a program to check Type of Triangle in C
  • Write a program to check Type of Triangle in C++
  • Write a program to check Type of Triangle in Python
  • Write a program to check Type of Triangle in PHP
  • Write a program to check Type of Triangle in Java
  • Write a program to check Type of Triangle in Java Script
  • Write a program to check Type of Triangle in C#

Explanation:

To determine the type of triangle based on its side lengths, you need to classify it into one of three categories:

  1. Equilateral Triangle: All three sides are equal.
  2. Isosceles Triangle: Exactly two sides are equal.
  3. Scalene Triangle: All three sides have different lengths.

Additionally, you can also check if the given sides form a valid triangle by verifying if the sum of the lengths of any two sides is greater than the length of the third side (Triangle Inequality Theorem).

Logic to Check the Type of Triangle

  1. Input: Three sides (let’s say a, b, and c).
  2. Check Triangle Validity:
    • A triangle is valid if:
      • a + b > c
      • a + c > b
      • b + c > a
  3. Determine the Type:
    • Equilateral Triangle: If a == b == c
    • Isosceles Triangle: If two sides are equal (a == b, b == c, or a == c).
    • Scalene Triangle: If all three sides are different (a ≠ b ≠ c).

Program to Check Type of Triangle

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter the three sides of the triangle: ");
    scanf("%d %d %d", &a, &b, &c);

    if (a == b && b == c) {
        printf("The triangle is Equilateral.\n");
    } else if (a == b || b == c || a == c) {
        printf("The triangle is Isosceles.\n");
    } else {
        printf("The triangle is Scalene.\n");
    }

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int a, b, c;
    cout << "Enter the three sides of the triangle: ";
    cin >> a >> b >> c;

    if (a == b && b == c) {
        cout << "The triangle is Equilateral." << endl;
    } else if (a == b || b == c || a == c) {
        cout << "The triangle is Isosceles." << endl;
    } else {
        cout << "The triangle is Scalene." << endl;
    }

    return 0;
}

a = int(input("Enter the first side of the triangle: "))
b = int(input("Enter the second side of the triangle: "))
c = int(input("Enter the third side of the triangle: "))

if a == b == c:
    print("The triangle is Equilateral.")
elif a == b or b == c or a == c:
    print("The triangle is Isosceles.")
else:
    print("The triangle is Scalene.")

<?php
$a = readline("Enter the first side of the triangle: ");
$b = readline("Enter the second side of the triangle: ");
$c = readline("Enter the third side of the triangle: ");

if ($a == $b && $b == $c) {
    echo "The triangle is Equilateral.\n";
} elseif ($a == $b || $b == $c || $a == $c) {
    echo "The triangle is Isosceles.\n";
} else {
    echo "The triangle is Scalene.\n";
}
?>

import java.util.Scanner;

public class TriangleType {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first side of the triangle: ");
        int a = scanner.nextInt();
        System.out.print("Enter the second side of the triangle: ");
        int b = scanner.nextInt();
        System.out.print("Enter the third side of the triangle: ");
        int c = scanner.nextInt();

        if (a == b && b == c) {
            System.out.println("The triangle is Equilateral.");
        } else if (a == b || b == c || a == c) {
            System.out.println("The triangle is Isosceles.");
        } else {
            System.out.println("The triangle is Scalene.");
        }

        scanner.close();
    }
}

let a = parseInt(prompt("Enter the first side of the triangle:"));
let b = parseInt(prompt("Enter the second side of the triangle:"));
let c = parseInt(prompt("Enter the third side of the triangle:"));

if (a === b && b === c) {
    console.log("The triangle is Equilateral.");
} else if (a === b || b === c || a === c) {
    console.log("The triangle is Isosceles.");
} else {
    console.log("The triangle is Scalene.");
}

using System;

class Program {
    static void Main() {
        Console.Write("Enter the first side of the triangle: ");
        int a = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter the second side of the triangle: ");
        int b = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter the third side of the triangle: ");
        int c = Convert.ToInt32(Console.ReadLine());

        if (a == b && b == c) {
            Console.WriteLine("The triangle is Equilateral.");
        } else if (a == b || b == c || a == c) {
            Console.WriteLine("The triangle is Isosceles.");
        } else {
            Console.WriteLine("The triangle is Scalene.");
        }
    }
}

List of All Programs