Write a Java program to check if three given side lengths (integers) can make a triangle or not

  • برمجة جافا
  • برمجة

Write a Java program to check if three given side lengths (integers) can make a triangle or not

Expected Output:

Input side1:  5
Input side2:  6
Input side3:  8
Is the said sides form a triangle: true 

الأجوبة

import java.util.*;
public class Solution {
 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  System.out.print("Input side1: ");
  int s1 = in .nextInt();
  System.out.print("Input side2: ");
  int s2 = in .nextInt();
  System.out.print("Input side3: ");
  int s3 = in .nextInt();

  System.out.print("Is the said sides form a triangle: " + isValidTriangle(s1, s2, s3));
 }
 public static boolean isValidTriangle(int a, int b, int c) {
  return (a + b > c && b + c > a && c + a > b);
 }
}

Sample Output:

Input side1:  5
Input side2:  6
Input side3:  8
Is the said sides form a triangle: true 
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...