{"id":22959,"date":"2021-09-29T20:13:34","date_gmt":"2021-09-29T14:43:34","guid":{"rendered":"https:\/\/python-programs.com\/?p=22959"},"modified":"2021-11-22T18:35:36","modified_gmt":"2021-11-22T13:05:36","slug":"python-program-to-check-whether-triangle-is-valid-or-not-if-sides-are-given","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-whether-triangle-is-valid-or-not-if-sides-are-given\/","title":{"rendered":"Python Program to Check Whether Triangle is Valid or Not if Sides are Given"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Triangular Matchstick Number<\/a> A triangle is said to be valid if the sum of its two sides is greater than the third side.<\/p>\n Conditions to check if the given triangle is valid or Not:<\/strong><\/p>\n Let a, b, c\u00a0 are the 3 sides of a triangle. It must satisfy the following conditions:<\/p>\n a + b > c<\/p>\n a + c > b<\/p>\n b + c > a<\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check if the given triangle is valid or not for the given 3 sides in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners<\/a> as our expert team has designed them from scratch.<\/p>\n In the previous article, we have discussed Python Program for Triangular Matchstick Number Given three sides of a triangle, the task is to check if the given triangle is valid or not for the given 3 sides in python. A triangle is said to be valid if the sum of its two sides is greater …<\/p>\n
\nGiven three sides of a triangle, the task is to check if the given triangle is valid or not for the given 3 sides in python.<\/p>\nGiven First side = 5\r\nGiven Second side = 7\r\nGiven Third side = 6<\/pre>\n
Yes, the triangle is valid for the given three sides<\/pre>\n
Given First side = 4\r\nGiven Second side = 9\r\nGiven Third side = 2<\/pre>\n
The triangle is invalid for the given three sides<\/pre>\n
Program to Check Whether Triangle is Valid or Not if Sides are Given in Python<\/h2>\n
\n
Method #1: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Give the first side of a triangle as static input and store it in a variable.\r\nfst_side = 5\r\n# Give the second side as static input and store it in another variable.\r\nscnd_side = 7\r\n# Give the third side as static input and store it in another variable.\r\nthrd_side = 6\r\n# Check if (fst_side + scnd_side <= thrd_side) or (fst_side + thrd_side <= scnd_side)\r\n# or (scnd_side + thrd_side <= fst_side) using the if conditional statement.\r\nif (fst_side + scnd_side <= thrd_side) or (fst_side + thrd_side <= scnd_side) or (scnd_side + thrd_side <= fst_side):\r\n # If it is true, then print \"The triangle is invalid for the given three sides\".\r\n print(\"The triangle is invalid for the given three sides\")\r\nelse:\r\n # Else print \"Yes, the triangle is valid for the given three sides\"\r\n print(\"Yes, the triangle is valid for the given three sides\")\r\n<\/pre>\n
Yes, the triangle is valid for the given three sides<\/pre>\n
Method #2: Using Mathematical Formula (User Input)<\/h3>\n
\n
# Give the first side of a triangle as user input using the int(input()) function and\r\n# store it in a variable.\r\nfst_side = int(input(\"Enter some random number = \"))\r\n# Give the second side as user input using the int(input()) function and\r\n# store it in another variable.\r\nscnd_side = int(input(\"Enter some random number = \"))\r\n# Give the third side as user input using the int(input()) function and\r\n# store it in another variable.\r\nthrd_side = int(input(\"Enter some random number = \"))\r\n# Check if (fst_side + scnd_side <= thrd_side) or (fst_side + thrd_side <= scnd_side)\r\n# or (scnd_side + thrd_side <= fst_side) using the if conditional statement.\r\nif (fst_side + scnd_side <= thrd_side) or (fst_side + thrd_side <= scnd_side) or (scnd_side + thrd_side <= fst_side):\r\n # If it is true, then print \"The triangle is invalid for the given three sides\".\r\n print(\"The triangle is invalid for the given three sides\")\r\nelse:\r\n # Else print \"Yes, the triangle is valid for the given three sides\"\r\n print(\"Yes, the triangle is valid for the given three sides\")\r\n<\/pre>\n
Enter some random number = 4\r\nEnter some random number = 9\r\nEnter some random number = 2\r\nThe triangle is invalid for the given three sides<\/pre>\n
\n