The Wayback Machine - https://web.archive.org/web/20240912193429/https://www.geeksforgeeks.org/check-whether-triangle-valid-not-sides-given/
Open In App

Check whether triangle is valid or not if sides are given

Last Updated : 20 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Easy
31.03%
4.2K

Given three sides, check whether triangle is valid or not. 
Examples: 
 

Input :  a = 7, b = 10, c = 5 
Output : Valid

Input : a = 1 b = 10 c = 12 
Output : Invalid

 

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. 
 

1.a + b > c 
2.a + c > b 
3.b + c > a  

 

Check whether triangle is valid or not if sides are given

 

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)
{
    // check condition
    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)
{
    // check condition
    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");
}
  
// This code is contributed by Aditya Kumar (adityakumar129)


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;
        // function calling and print output
        if ((checkValidity(a, b, c)) == 1)
            System.out.print("Valid");
        else
            System.out.print("Invalid");
    }
}
  
// This code is contributed by Aditya Kumar (adityakumar129)


Python3




# 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.


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";
          
// This code is contributed by nitin mittal.
?>


Javascript




<script>
  
// 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) 
{ 
    // check condition 
    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");     
  
// This code is contributed by Mayank Tyagi
  
</script>


Output

Valid

Time Complexity: O(1)
Auxiliary Space: O(1)



Similar Reads

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 &lt;= a, b, c &lt;= 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 &lt; 13 which violates the property of triangle. Hence print "No"
8 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 three angles is equal to 180 degrees. Below is the
3 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 points form a triangle only when they don't lie on
9 min read
Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle
Given an array arr[] consisting of N integers, the task is to find the size of the largest subset of the array such that a triangle can be formed from any of the three integers of the subset as the sides of a triangle. Examples: Input: arr[] = {1, 4, 7, 4}Output: 3Explanation: A possible subsets that follow the given conditions are {1, 4, 4} and {4
7 min read
Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
Given N-sided polygon we need to find the total number of triangles formed by joining the vertices of the given polygon with exactly two sides being common and no side being common.Examples: Input : N = 6 Output : 6 2 The image below is of a triangle forming inside a Hexagon by joining vertices as shown above. The triangle formed has two sides (AB
5 min read
Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
Given three integers N, X and Y. The task is to find the number of ways to arrange 2*N persons along two sides of a table with N number of chairs on each side such that X persons are on one side and Y persons are on the opposite side. Note: Both X and Y are less than or equals to N.Examples: Input : N = 5, X = 4, Y = 2 Output : 57600 Explanation :
9 min read
Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
Given a binary matrix arr[][] of dimensions N * M , the task is to count the number of right-angled triangles that can be formed by joining the cells containing the value 1 such that the triangles must have any two of its sides parallel to the sides of the rectangle. Examples: Input: arr[][] = {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}Output: 4Explanation:
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), B = (10, 30), C = (20, 0), P(30, 15)Output: Out
15+ min read
Find area of triangle if two vectors of two adjacent sides are given
Given two vectors in form of (xi+yj+zk) of two adjacent sides of a triangle. The task is to find out the area of a triangle.Examples: Input: x1 = -2, y1 = 0, z1 = -5 x2 = 1, y2 = -2, z2 = -1 Output: Area = 6.422616289332565Input: x1 = -2, y1 = 1, z1 = 5 x2 = 1, y2 = 3, z2 = -1 Output: Area = 8.860022573334675 Approach: Suppose we have two vectors a
5 min read
Length of remaining two sides of a Triangle from a given side and its adjacent angles
Given the length of a side a of a triangle and its adjacent angles B and C, the task is to find the remaining two sides of the triangle. Input: a = 5, B = 62.2, C = 33.5 Output: 4.44, 2.77 Explanation The remaining two sides of the triangle are b = 4.44488228556699 and c = 2.7733977979419038Input: a = 12, B = 60, C = 30 Output: 10.39, 5.99 Explanat
5 min read
Queries to count points lying on or inside an isosceles Triangle with given length of equal sides
Given an array arr[][] of dimension N * 2 representing the co-ordinates of N points and an array Q[] consisting of M integers, the task for every element in the Q[i] is to find the number of points lying inside or on the right-angled isosceles triangle formed on the positive co-ordinate axis with two equal sides of length Q[i]in each query. Example
7 min read
Maximize area of triangle formed by points on sides of given rectangle
Given a rectangle [(x1, y1), (x2, y2)] denoting the coordinates of bottom-left corner and top-right corner whose sides are parallel to coordinates axes and N points on its perimeter (at least one on each side). The task is to maximize the area of a triangle formed by these points. Examples: Input: rectangle[][]= {{0, 0}, {6, 6}}, coordinates[][] =
10 min read
Find the type of triangle from the given sides
Given three integers A, B, and C which denote the sides of a triangle, the task is to check that the triangle is a right-angled, acute-angled, or obtuse-angled triangle.Examples: Input: A = 1, B = 4, C = 3 Output: Obtuse-angled Triangle Explanation: Triangle with the sides 1, 2 and 3 is an obtuse-angled triangleInput: A = 2, B = 2, C = 2 Output: Ac
5 min read
Find the length of the median of a Triangle if length of sides are given
Given the length of all three sides of a triangle as a, b and c. The task is to calculate the length of the median of the triangle.  A median of a triangle is a line segment joining a vertex to the midpoint of the opposite side, thus bisecting that side.   Examples:   Input: a = 8, b = 10, c = 13 Output: 10.89Input: a = 4, b = 3, c = 5 Output: 3.61
4 min read
Find all sides of a right angled triangle from given hypotenuse and area | Set 1
Given hypotenuse and area of a right angle triangle, get its base and height and if any triangle with given hypotenuse and area is not possible, print not possible.Examples: Input : hypotenuse = 5, area = 6 Output : base = 3, height = 4 Input : hypotenuse = 5, area = 7 Output : No triangle possible with above specification. Recommended PracticeArea
9 min read
Check whether the given string is a valid identifier
Given a string str, the task is to check if the string is a valid identifier or not. In order to qualify as a valid identifier, the string must satisfy the following conditions: It must start with an either underscore(_) or any of the characters from the ranges ['a', 'z'] and ['A', 'Z'].There must not be any white space in the string.And, all the s
6 min read
Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
Given a string S of valid parentheses sequence of length N and an even integer K, the task is to find the valid parentheses sequence of length K which is also a subsequence of the given string. Note: There can be more than one valid sequence, print any of them. Examples: Input: S = "()()()", K = 4Output: ()()Explanation:The string "()()" is a subse
7 min read
Probability of cutting a rope into three pieces such that the sides form a triangle
Given a rope. We have to find the probability of cutting a rope into 3 pieces such that they form a triangle. Answer: 0.25 Explanation: Let the length of rope be 1 unit. We choose two points X and Y on the rope. Note: Formation of triangle is based on Triangle inequality i.e. sum of the lengths of any two sides of a triangle must be greater than th
2 min read
Find other two sides and angles of a right angle triangle
Given one side of right angle triangle, check if there exists a right angle triangle possible with any other two sides of the triangle. If possible print length of the other two sides and all the angles of the triangle. Examples: Input : a = 12 Output : Sides are a = 12, b = 35, c = 37 Angles are A = 18.9246, B = 71.0754, C = 90 Explanation: a = 12
13 min read
Minimum increment in the sides required to get non-negative area of a triangle
Given three sides of the triangle, find the minimum increase in the length of the side of the triangle to make the area of the triangle non-negative. Examples: Input: a = 3, b = 4, c = 10 Output: 3 With the given sides, the area is negative. If a is increased to 5 and b to 5, then the area becomes 0, which is not negative. Input: a = 6, b = 6, c =
5 min read
Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
Given two integers A, B representing the length of two sides of a triangle and an integer K representing the angle between them in radian, the task is to calculate the area of the triangle from the given information.Examples: Input: a = 9, b = 12, K = 2 Output: 49.1 Explanation: Area of triangle = 1 / 2 * (9 * 12 * Sin 2) = 35.12Input: A = 2, B = 4
4 min read
Calculate area and height of an isosceles triangle whose sides are radii of a circle
Given integers R and [Tex]\alpha [/Tex]representing the radius of a circle and the angle formed at the center O by the sector AB (as shown in the figure below), the task is to find the height and area of the triangle formed by connecting the points A, B, and O if possible. Otherwise, print "Not possible". Examples: Input: R = 5, [Tex]\alpha [/Tex]=
6 min read
Area of a triangle with two vertices at midpoints of opposite sides of a square and the other vertex lying on vertex of a square
Given a positive integer N representing the side of a square, the task is to find the area of a triangle formed by connecting the midpoints of two adjacent sides and vertex opposite to the two sides. Examples: Input: N = 10Output: 37.5 Input: N = 1Output: 0.375 Approach: The given problem can be solved based on the following observations: The one s
5 min read
Sort the sides of triangle on the basis of increasing area
Given an array arr[] of sides of N triangles, the task is to sort the given sides of triangles on the basis of the increasing order of area. Examples: Input: arr[] = {{5, 3, 7}, {15, 20, 4}, {4, 9, 6}, {8, 4, 5}}Output: {{5, 3, 7}, {8, 4, 5}, {4, 9, 6}, {15, 20, 17}}Explanation:Following are the areas of triangle: Area of 1st triangle (5, 3, 7) is
11 min read
Find other two sides of a right angle triangle
Given one side of right angle triangle, check if there exists a right angle triangle possible with any other two sides of the triangle. If possible print length of the other two sides. Else print -1 Note: All the sides of triangle must be positive integers Example 1: Input : a = 3 Output : b = 4, c = 5 Explanation : a = 3, b = 4 and c = 5 form righ
7 min read
Largest Area of Triangle with sides parallel to axes
Given N distinct points[][], the task is to find the maximum area of a triangle that can be formed having one of the sides parallel to X axis and another pair of sides parallel to Y axis. Examples: Input: N = 4, points[][] = {{1, 1}, {1, 4}, {3, 1}, {5, 1}}Output: 6Explanation: The points {1, 1}, {1, 4} and {5, 1} forms a triangle with area = (1/2)
8 min read
Theorem - Angle opposite to equal sides of an isosceles triangle are equal | Class 9 Maths
In geometry, an isosceles triangle is a triangle that has two sides of equal length. Sometimes it is specified as having exactly two sides of equal length, and sometimes as having at least two sides of equal length, the latter version thus including the equilateral triangle as a special case. Examples of isosceles triangles include the isosceles ri
4 min read
Check if given Sudoku board configuration is valid or not
Given a Sudoku Board configuration, check whether it is valid or not. Examples: Input: [5 3 - - 7 - - - -] [6 - - 1 9 5 - - -] [- 9 8 - - - - 6 -] [8 - - - 6 - - - 3] [4 - - 8 - 3 - - 1] [7 - - - 2 - - - 6] [- 6 - - - - 2 8 -] [- - - 4 1 9 - - 5] [- - - - 8 - - 7 9] Output: True Input: [3 9 - 7 5 2 - - -] [5 - 4 - - - - 7 -] [- - 2 - 8 - - 9 -] [1
14 min read
Check if the given chessboard is valid or not
Given an NXN chessboard. The task is to check if the given chessboard is valid or not. A chess board is considered valid if every 2 adjacent cells are painted with different color. Two cells are considered adjacent if they share a boundary. First chessboard is valid whereas the second is invalid. Examples: Input : N = 2 C = { { 1, 0 }, { 0, 1 } } O
10 min read
Check if the given Prufer sequence is valid or not
Given a Prufer sequence of N integers, the task is to check if the given sequence is a valid Prufer sequence or not.Examples: Input: arr[] = {4, 1, 3, 4} Output: Valid The tree is: 2----4----3----1----5 | 6 Input: arr[] = {4, 1, 7, 4} Output: Invalid Approach: Since we know the Prufer sequence is of length N - 2 where N is the number of vertices. H
7 min read
Practice Tags :