Java Code To Check if Triangle is Valid

By | May 15, 2016

Check if Triangle is Valid or Not in Java Programming

Here’s a Code to Check if Triangle is Valid or Not in Java Programming Language. This is a simple C code that takes Triangle Sides as the Input from the User and compares all the Sides. The Code then Checks the Validity of the Triangle with If – Else Block Structure.

A Triangle is Valid only if one of the three sides of the triangle is greater than the other two sides. The Triangle Validity Test will find if the Triangle is valid or not which includes checking the following condition checking:

  • First Side + Third Side > Second Side
  • Third Side + Second Side > First Side
  • Second Side + First Side > Third Side

Also Read: Java Program To Convert Numbers To Words

Method 1: Java Program To Check The Validity Of A Triangle

import java.io.*;
import java.util.*;

class triangle_validity
{
    public static void main(String args[])
    {
        float l1, l2, l3;
        Scanner sc = new Scanner(System.in);
        System.out.println("\nLength of First Side: \t");
        l1 = sc.nextFloat();
        System.out.println("\nLength of Second Side: \t");
        l2 = sc.nextFloat();
        System.out.println("\nLength of Third Side: \t");
        l3 = sc.nextFloat();
        if ((l1 + l2) > l3)
        {
            if((l1 + l3) > l2)
            {
                if(l2 + l3 > l1)
                {
                    System.out.println("\nThe Triangle is a Valid Triangle\n\n");
                }
                else
                {
                    System.out.println("\nThe Triangle is Not a Valid Triangle\n\n");
                }
            }
            else
            {
                System.out.println("\nThe Triangle is Not a Valid Triangle\n\n");
            }
        }
        else
        {
            System.out.println("\nThe Triangle is Not a Valid Triangle\n\n");
        }
    }
}

Method 2: Check If Triangle Is Valid Or Not In Java Programming

import java.io.*;
import java.util.*;

import java.io.*;
import java.util.*;

class triangle_validity
{
    int l1, l2,l3;
    public triangle_validity(int a, int b, int c)
    {
        l1 = a;
        l2 = b;
        l3 = c;
    }
   
    public void check_validity()
    {
        if ((l1 + l2) > l3)
        {
            if((l1 + l3) > l2)
            {
                if(l2 + l3 > l1)
                {
                    System.out.println("\nThe Triangle is a Valid Triangle\n\n");
                }
                else
                {
                    System.out.println("\nThe Triangle is Not a Valid Triangle\n\n");
                }
            }
            else
            {
                System.out.println("\nThe Triangle is Not a Valid Triangle\n\n");
            }
        }
        else
        {
            System.out.println("\nThe Triangle is Not a Valid Triangle\n\n");
        }
    }
}

class Demo
{
    public static void main(String args[])
    {
        int a, b, c;
        Scanner sc = new Scanner(System.in);
        System.out.println("\nLength of First Side: \t");
        a = sc.nextInt();
        System.out.println("\nLength of Second Side: \t");
        b = sc.nextInt();
        System.out.println("\nLength of Third Side: \t");
        c = sc.nextInt();
        triangle_validity obj1 = new triangle_validity(a,b,c);
        obj1.check_validity();
    }
}

Output

Java Program To Check if Triangle is valid or not

Also Read: Java Code To Draw A Circle using AWT and Graphics

 

If you have any compilation erros or about in this Code To Check if Triangle is Valid or Not in Java Programming, you can write about it in the Comment Section below.

Let's Discuss