Write java program to accept the three sides of a triangle and check if it is possible to form a triangle with these values or not. The condition for forming a triangle is that the sum of any two sides is greater than the third side . Do not accept any negative number or 0 as input and terminate program in case of invalid input.
Answers
Answer:
import java.util.Scanner;
public class IsTrianglePossible
{
public static void main(String[] args)
{
//Declaration
Scanner input = new Scanner(System.in);
double a;
double b;
double c;
//Prompt and accept 3 sides from user
System.out.print("Enter first side: ");
a = input.nextDouble();
System.out.print("Enter second side: ");
b = input.nextDouble();
System.out.print("Enter third side: ");
c = input.nextDouble();
//Check whether a triangle is possible and display appropriate
//message.
if (a == 0 || a <= 0 && b == 0 || b <= 0 && c == 0 || c <= 0)
{
System.exit(0);
}
if(a + b > c && b + c > a && a + c > b)
{
System.out.println("Triangle is possible.");
}
else
{
System.out.println("Triangle is not possible.");
}
}
}