Write a program that reads 3 edges of a triangle and computes the perimeter if i
ID: 3751098 • Letter: W
Question
Write a program that reads 3 edges of a triangle and computes the perimeter if it is valid in JAVA
Input:
•Get the 3 edges as user input
Calculations:
•The inputs are valid if the sum of every pair of two edges is greater than the third edge. Use logical AND for calculations and store the output as a Boolean variable
•If it is valid(Boolean) calculate the perimeter
Output:
•If it is valid calculate the perimeter
•Otherwise print It is invalid
Explanation / Answer
import java.util.Scanner; public class TrianglePerimeter { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter lengths of 3 sides of triangle: "); double s1 = in.nextDouble(); double s2 = in.nextDouble(); double s3 = in.nextDouble(); boolean isValid = (s1+s2 > s3 && s2+s3 > s1 && s1+s3 > s2); if(isValid) { System.out.println("Perimeter of triangle is " + (s1+s2+s3)); } else { System.out.println("Triangle is invalid!"); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.