Coding Practice

Write a Java program to input all sides of a triangle and check whether triangle is valid or not

Input all sides of a triangle and check whether triangle is valid or not
Sample Output
Input all sides of a triangle and check whether triangle is valid or not.

Enter three sides...
5
7
6
Triangle is valid.
Java-Source Code
//package ifelsejava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int side1, side2, side3;

        Scanner input = new Scanner(System.in);

        System.out.println("Input all sides of a triangle and check whether triangle is valid or not.\n");

        System.out.println("Enter three sides...");
        side1 = input.nextInt();
        side2 = input.nextInt();
        side3 = input.nextInt();

        if ((side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1)) {
            System.out.println("Triangle is valid.");
        
        } else {
            System.out.println("Triangle is not valid.");
            
        }
    }
}
Sample Output
Input all sides of a triangle and check whether triangle is valid or not.

Enter three sides...
2
3
5
Triangle is not valid.

No comments:

Post a Comment

Change Theme
X