Latest :

Java Program : Maximum Edge of A Triangle | Java Programs

Java program to calcuate the maximium edge of triangle, Our problem statement here is, to find the longest side or edge of a triangle. For this, our required input will be, the lengths of three sides or edges of a triangle. Our expected output is, one of these three edges that is the longest of all.

Initially, to read the three edges at runtime, we can make use of the Scanner class in Java. This class reads any input from the console screen at runtime. To make use of this class, we first create an object instantiating the class and then call the necessary method to read a primitive datatype value. Here, our edges are of integer type so, we invoke the nextInt() method as shown below:

After reading the three edges, we need to start comparing it with each other to find the largest among them.

First we see if, edge1 (e1) is greater than edge2(e2). If yes then, we check whether edge1 (e1) is greater than edge3 (e3) too and if yes then, edge1 (e1) is the longest and we store this in a variable (max). If egde1 (e1) is not greater than edge3 (e3) then, edge3 (e3) is the longest and will be stored in the variable (max).

if(e1>=e2) {






Now, if edge1 (e1) is not greater than edge2 (e2) then, we check whether edge2 (e2) is greater than edge3 (e3). If yes then, edge2 (e2) is the longest and is stored in the max variable and else, edge3 (e3) is the greatest and is stored in the max variable.

else if(e2>=e3) {





Our desired output i.e., the longest edge among the three is stored in this max variable and can be displayed on the console screen by making use of System.out.println() method.

System.out.println(“The maximum edge of a trinagle of 3 edges (“+e1+” “+e2+” “+e3+”) is :”+max);


Here, e1=4, e2=3 and e3=5,

e1>=e2 i.e., 4>=3 yes so,

e1>=e3 i.e., 4>=5 no so, max=e3=5.

Static Method – Edge of Triangle Java

This method also has the exact same logic and set of statements as discussed above but the difference is that, the main logic with comparison of three edges is placed within a separate static method (findMaxEdge) which takes three edges as arguments and returns the longest edge (m) to the main function.

We do this so that, we can make this part of the code reusable. If the same logic were to be used elsewhere in the code later then, instead of rewriting the same set of statements we can just make a method call and pass the arguments.

Not just that, it also enhances the readability of the code and makes it clearer. Also, for a static method, we do not need to create an object despite being outside main method. This is because, static method belongs to the class and not the instance.


s1=20, s2=20 and s3=20.

s1>=s2 i.e., 20>=20 yes so,

s1>=s3 i.e., 20>=20 yes then, m=e1=20.


Check : Transpose Matrix In Java


Check Also

Rhombus Star Pattern Program In Java – Patterns

Java program to print Rhombus star pattern program. We have written the below print/draw Rhombus ...