Check whether triangle is valid or not if sides are given Last Updated : 10 Feb, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given three sides, check whether triangle is valid or not. Examples: Input : a = 7, b = 10, c = 5 Output : ValidWe can draw a triangle with the given three edge lengths.Input : a = 1, b = 10, c = 12 Output : InvalidWe can not draw a triangle with the given three edge lengths.Approach: A triangle is valid if sum of its two sides is greater than the third side. If three sides are a, b and c, then three conditions should be met. (a + b) > c(a + c) > b(b + c) > a C++ // C++ program to check if three sides form a triangle or not #include <bits/stdc++.h> using namespace std; // function to check if three sider form a triangle or not bool checkValidity(int a, int b, int c) { if (a + b <= c || a + c <= b || b + c <= a) return false; else return true; } // Driver function int main() { int a = 7, b = 10, c = 5; if (checkValidity(a, b, c)) cout << "Valid"; else cout << "Invalid"; } // This code is contributed by Aditya Kumar (adityakumar129) C // C program to check if three sides form a triangle or not #include <stdio.h> #include <stdbool.h> // function to check if three sider form a triangle or not bool checkValidity(int a, int b, int c) { if (a + b <= c || a + c <= b || b + c <= a) return false; return true; } // Driver function void main() { int a = 7, b = 10, c = 5; if (checkValidity(a, b, c)) printf("Valid"); else printf("Invalid"); } Java // Java program to check validity of any triangle public class GFG { // Function to calculate for validity public static int checkValidity(int a, int b, int c) { // check condition if (a + b <= c || a + c <= b || b + c <= a) return 0; else return 1; } // Driver function public static void main(String args[]) { int a = 7, b = 10, c = 5; if ((checkValidity(a, b, c)) == 1) System.out.print("Valid"); else System.out.print("Invalid"); } } Python # Python3 program to check if three # sides form a triangle or not # function to check if three sides # form a triangle or not def checkValidity(a, b, c): # check condition if (a + b <= c) or (a + c <= b) or (b + c <= a) : return False else: return True # driver code a = 7 b = 10 c = 5 if checkValidity(a, b, c): print("Valid") else: print("Invalid") C# // C# program to check // validity of any triangle using System; class GFG { // Function to calculate for validity public static int checkValidity(int a, int b, int c) { // check condition if (a + b <= c || a + c <= b || b + c <= a) return 0; else return 1; } // Driver code public static void Main() { int a = 7, b = 10, c = 5; // function calling and print output if ((checkValidity(a, b, c)) == 1) Console.Write("Valid"); else Console.Write("Invalid"); } } // This code is contributed by Nitin Mittal. JavaScript // Javascript program to check if three // sides form a triangle or not // function to check if three sider // form a triangle or not function checkValidity(a, b, c) { if (a + b <= c || a + c <= b || b + c <= a) return false; else return true; } // Driver function let a = 7, b = 10, c = 5; if (checkValidity(a, b, c)) document.write("Valid"); else document.write("Invalid"); PHP <?php // PHP program to check if three // sides form a triangle or not // function to check if three sider // form a triangle or not function checkValidity($a, $b, $c) { // check condition if ($a + $b <= $c || $a + $c <= $b || $b + $c <= $a) return false; else return true; } // Driver Code $a = 7; $b = 10; $c = 5; if (checkValidity($a, $b, $c)) echo "Valid"; else echo "Invalid"; ?> OutputValidTime Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check whether triangle is valid or not if three points are given S Striver Follow Improve Article Tags : Misc Geometric DSA Basic Coding Problems triangle +1 More Practice Tags : GeometricMisc Similar Reads Check whether triangle is valid or not if three points are given Given coordinates of three points in a plane P1, P2 and P3, the task is to check if the three points form a triangle or notExamples: Input: P1 = (1, 5), P2 = (2, 5), P3 = (4, 6) Output: YesInput: P1 = (1, 1), P2 = (1, 4), P3 = (1, 5) Output: No Approach: The key observation in the problem is three p 9 min read Check whether triangle is valid or not if three points are given Given coordinates of three points in a plane P1, P2 and P3, the task is to check if the three points form a triangle or notExamples: Input: P1 = (1, 5), P2 = (2, 5), P3 = (4, 6) Output: YesInput: P1 = (1, 1), P2 = (1, 4), P3 = (1, 5) Output: No Approach: The key observation in the problem is three p 9 min read Check whether the triangle is valid or not if angles are given Given three integers A, B and C which are the three angles of a possible triangle in degrees, the task is to check whether the triangle is valid or not.Examples: Input: A = 60, B = 40, C = 80 Output: ValidInput: A = 55, B = 45, C = 60 Output: Invalid Approach: A triangle is valid if the sum of the t 3 min read Check whether right angled triangle is valid or not for large sides Given three integers a, b and c as triplets. Check if it is possible to make right angled triangle or not. Print Yes if possible, else No. 10-18 <= a, b, c <= 1018 Examples: Input: 3 4 5 Output: Yes Explanation: Since 3*3 + 4*4 = 5*5 Hence print "Yes" Input: 8 5 13 Since 8 + 5 < 13 which vi 8 min read Check whether a given point lies inside a triangle or not Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not. Example: Input: A = (0, 0), B = (10, 30), C = (20, 0), P(10, 15)Output: InsideExplanation: B(10,30) / \ / \ / \ / P \ P' / \ A(0,0) ----------- C(20,0) Input: A = (0, 0 15+ min read Check whether a given point lies inside a rectangle or not Given four points of a rectangle, and one more point P. Write a function to check whether P lies within the given rectangle or not.Examples:Â Â Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)] P = (0, 0)Output : yesIllustration :Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)], P = (20 14 min read Like