Valid Triangle Check: flowchart, algorithm and program

This program reads three sides a, b and c and tells you whether they can form a triangle. It uses the triangle inequality: the sum of any two sides must be greater than the third side.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read a, b, c.
  3. Step 3: If a+b>c and a+c>b and b+c>a, go to step 4; otherwise go to step 5.
  4. Step 4: Print "Valid triangle". Go to step 6.
  5. Step 5: Print "Not a valid triangle".
  6. Step 6: Stop.

Pseudocode

BEGIN
    READ a, b, c
    IF a+b>c and a+c>b and b+c>a THEN
        PRINT "Valid triangle"
    ELSE
        PRINT "Not a valid triangle"
    END IF
END

Program in C, Python and Java

Valid Triangle Check program in C

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter three sides: ");
    scanf("%d %d %d", &a, &b, &c);
    if (a + b > c && a + c > b && b + c > a)
        printf("Valid triangle\n");
    else
        printf("Not a valid triangle\n");
    return 0;
}

Valid Triangle Check program in Python

a = int(input("Enter side a: "))
b = int(input("Enter side b: "))
c = int(input("Enter side c: "))
if a + b > c and a + c > b and b + c > a:
    print("Valid triangle")
else:
    print("Not a valid triangle")

Valid Triangle Check program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter three sides: ");
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        if (a + b > c && a + c > b && b + c > a)
            System.out.println("Valid triangle");
        else
            System.out.println("Not a valid triangle");
        sc.close();
    }
}

Sample input: a = 3, b = 4, c = 5
Output: Enter three sides: Valid triangle

How it works

  • scanf reads the three side lengths into a, b and c.
  • The if condition checks all three pairs: a + b > c, a + c > b and b + c > a.
  • The && operator means every one of the three checks must be true. If even one fails, the sides cannot close up into a triangle.
  • If all checks pass, it prints "Valid triangle"; otherwise the else branch prints "Not a valid triangle".
  • For a = 3, b = 4, c = 5: 3+4=7 > 5, 3+5=8 > 4 and 4+5=9 > 3, so all are true and the output is "Valid triangle".

Dry run (trace table)

StepLineBoxabcOutput
13Start
26Read a, b, c345Enter three sides:
37a+b>c and a+c>b and b+c>a?345
48Print "Valid triangle"345Valid triangle
511Stop345

Time and space complexity

Time: O(1) · Space: O(1). It performs a fixed number of comparisons on three integers, with no loops.

Common mistakes

  • Checking only one pair, such as a + b > c. All three conditions are needed, because the longest side could be any of a, b or c.
  • Using >= instead of >. If a + b == c the sides lie flat on one line (a degenerate triangle), so it is not valid.
  • Using || instead of &&, which would accept sides like 1, 2, 10 because one of the checks happens to pass.
  • Not rejecting zero or negative sides. This program only checks the sums, so you may want to add a check that a, b and c are all positive.

Viva questions

Q1. What is the rule your program uses to decide whether three sides form a triangle?

The triangle inequality: the sum of any two sides must be strictly greater than the third side. Line 7 checks this for all three pairs.

Q2. Why do you check all three conditions on line 7 and not just a + b > c?

Any one of the sides could be the longest. If you only check one pair, a very long a or b can still break the rule, for example 10, 2, 3: a + b > c is true but a + c > b is false.

Q3. Why is the logical AND (&&) used and not OR (||)?

All three conditions must be true together for a valid triangle. With OR, a single true condition would be enough, and invalid sides like 1, 2, 10 could pass.

Q4. What does your program print for the sides 1, 2, 3? Why?

It prints 'Not a valid triangle'. Here 1 + 2 = 3, which is not greater than 3, so the condition fails. The sides would lie flat on a line and form a degenerate triangle.

Q5. What happens if the user enters 0 or a negative number, like 0, 5, 5 or -2, 5, 5?

For 0, 5, 5 it correctly says not valid, since 0 + 5 > 5 is false. But -2, 5, 5 gives true for all three checks, so the program wrongly says valid. The program does not check that each side is positive.

Q6. What would you change to make the program reject non-positive sides?

Add a check such as a > 0 && b > 0 && c > 0 to the condition on line 7, or test it first and print an error message. It would also help to check the return value of scanf.

Q7. What is the time complexity of this program?

It is O(1). It does a fixed number of comparisons and additions with no loops, regardless of the values entered.

Q8. What would change if you wanted to also tell whether the triangle is equilateral, isosceles or scalene?

After the validity check passes, compare the sides. If a == b && b == c it is equilateral. Else if any two sides are equal it is isosceles. Otherwise it is scalene. Only the code inside the valid branch changes.

Frequently asked questions

What is the condition for three sides to form a triangle?

The sum of every pair of sides must be greater than the remaining side. In code that means a + b > c, a + c > b and b + c > a must all be true.

Why do we check all three conditions instead of just the largest side?

Checking all three works without first finding the largest side. If you do find the largest side, one check (the two smaller sides summed against the largest) is enough, but that needs extra code.

Are the sides 2, 3, 5 a valid triangle?

No. 2 + 3 = 5, which is not greater than 5, so the check a + b > c fails. The sides would lie flat on a straight line.

More programs

← All programs

Try AI Flowchart free →