Write a program to input three angles of a triangle and check whether a triangle is impossible or not . If possible then check whether it is an acute angled triangle , right angled triangle or an obtuse angled triangle otherwise, display 'Triangle not possible'.
Sample input : Enter the angles: 40, 50 , 90
Sample output: Right=angled triangle
Answers
import java.util.*;
class Dcoder
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the three angles");
int a1=sc.nextInt();
int a2=sc.nextInt();
int a3=sc.nextInt();
if((a1+a2+a3)==180)
{
System.out.println("it is a valid triangle");
if(a1<90 && a2<90 && a3<90)
System.out.println("it is an acute angled triangle");
else if(a1>90 || a2>90 || a3>90)
System.out.println("it is an obtuse angled triangle");
else
System.out.println("it is a right angled triangle");
}
else
System.out.println("it is not valid");
}
}
Answer:
package com.akshay;
import java.util.Scanner;
public class Triangle{
public static void main(String[] args) {
System.out.println("Enter theree angles for triangle:");
int x,y,z;
String type = null;
Scanner sc= new Scanner(System.in);
x=sc.nextInt();
y=sc.nextInt();
z=sc.nextInt();
if(x+y+z==180&&(x!=0&&y!=0&&z!=0))
/** Since a triangle's angles must sum to 180° in Euclidean geometry, no Euclidean triangle can have more than one obtuse angle.
* As well as no angle should be equal to Zero */
{
if(x<90||y<90||z<90)
//An acute triangle (or acute-angled triangle) is a triangle with three acute angles (less than 90°).
{
type="Acute angled Triangle";
}
if(x==90||y==90||z==90)
//A right triangle is a triangle in which one angle is a right angle (that is, a 90-degree angle).
{
type="Right angled Triangle";
}
if(x>90||y>90||z>90)
//An obtuse triangle (or obtuse-angled triangle) is a triangle with one obtuse angle (greater than 90°) and two acute angles.
{
type="Obtuse angled Triangle";
}
}
else {
type="Triangle not possible";
}
System.out.println(type);
sc.close();
}
}
Explanation:
in previous answer validation for zero was not available, yes the program was simple but zero validation is one of the important point