Solution in java ONLY plz... FULL question can be found on the following link http://ntci.on.ca/compsci/java/ch5/5_9.html ---> question 12
----------------------------------------------------------------------------------
Write a program that will read an arbitrary number of sets of three integers. The program should prompt the user for sets of numbers and process them until the user submits the numbers 0 0 0. For each set of three numbers, the program should first print the values read. It should then decide whether or not the three numbers could represent the lengths of the sides of a triangle. If the numbers could not represent the lengths of sides of a triangle, an appropriate message should be printed. If they could, then the program should determine and state into which of the above classes the triangle would be placed.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("Provide three side lengths - 0 0 0 to terminate.");
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
if (a == 0 && b == 0 && c == 0) {
System.out.println("Program was terminated by user.");
break;
}
// make a the biggest side
if (a < b) {
int tmp = a;
a = b;
b = tmp;
}
if (a < c) {
int tmp = a;
a = c;
c = tmp;
}
if (a <= 0 || b <= 0 || c <= 0 || a >= b + c) {
System.out.println("Triangle cannot be formed.");
}
else {
String side, angle;
if (a == b && a == c)
side = "equilateral";
else if (a == b || a == c || b == c)
side = "isosceles";
else
side = "scalene";
int diff = a * a - b * b - c * c;
if (diff == 0)
angle = "right";
else if (diff > 0)
angle = "obtuse";
else
angle = "acute";
System.out.println("Triangle possible: " + side + " and " + angle + ".");
}
}
}
}
Comments
Leave a comment