Program to Check if Triangle is Valid or Not using Sides
- Write a program to check if Triangle is Valid or Not using sides in C
- Write a program to check if Triangle is Valid or Not using sides in C++
- Write a program to check if Triangle is Valid or Not using sides in Python
- Write a program to check if Triangle is Valid or Not using sides in PHP
- Write a program to check if Triangle is Valid or Not using sides in Java
- Write a program to check if Triangle is Valid or Not using sides in Java Script
- Write a program to check if Triangle is Valid or Not using sides in C#
Explanation:
To determine if a triangle is valid based on its sides, you can apply the Triangle Inequality Theorem.
Triangle Inequality Theorem
The theorem states that for any triangle with sides a, b, and c, the following conditions must be satisfied for the triangle to be valid:
- a + b > c
- a + c > b
- b + c > a
If all of these conditions hold true, then the triangle is valid. Otherwise, it is not a valid triangle.
Logic to Check Validity of Triangle Using Sides
- Input the lengths of the three sides: a, b, and c.
- Check if the sum of any two sides is greater than the third side (Triangle Inequality Theorem).
- Output whether the triangle is valid or not based on the conditions.
Program to Check if Triangle is Valid or Not using Angles
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#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 > c) && (a + c > b) && (b + c > a)) { printf("The triangle is valid.\n"); } else { printf("The triangle is not valid.\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 > c) && (a + c > b) && (b + c > a)) { cout << "The triangle is valid." << endl; } else { cout << "The triangle is not valid." << 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) and (a + c > b) and (b + c > a): print("The triangle is valid.") else: print("The triangle is not valid.")
<?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 > $c) && ($a + $c > $b) && ($b + $c > $a)) { echo "The triangle is valid.\n"; } else { echo "The triangle is not valid.\n"; } ?>
import java.util.Scanner; public class TriangleValidity { 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 > c) && (a + c > b) && (b + c > a)) { System.out.println("The triangle is valid."); } else { System.out.println("The triangle is not valid."); } 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 > c) && (a + c > b) && (b + c > a)) { console.log("The triangle is valid."); } else { console.log("The triangle is not valid."); }
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 > c) && (a + c > b) && (b + c > a)) { Console.WriteLine("The triangle is valid."); } else { Console.WriteLine("The triangle is not valid."); } } }