Java Program to Find Perimeter of Triangle

In the previous article we have discussed Java Program to Find Area of Triangle

In this article we will see different ways to find the perimeter of a triangle.

Program to Find Perimeter of Triangle

Before jumping into the program directly, first let’s understand how we get the perimeter of a triangle.

Formula for Perimeter of Circle: side1+side2+side3

Means it is the addition of  lengths of three sides of the triangle.

Example:

Example-1

When length of 3 sides of triangle are
side1= 3
side2= 4
side3= 5
Perimeter = 12
Example-2

When length of 3 sides of triangle are
side1= 3.1
side2= 4.2
side3= 5.1
Perimeter = 12.4

Different ways to find the perimeter of triangle.

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Method-1: Java Program to Find Perimeter of Triangle Using static value

In this we have mentioned the length for 3 sides of the triangle as static values in the program. Then these lengths will be used to find the perimeter using the formula.

Let’s see the program to understand it more clearly.

import java.util.*;

class Main 
{
    // Driver method
    public static void main(String[] args)
    {
        //length of 3 sides of triangle declared
        double side1 = 2.0; 
        double side2 = 3.0; 
        double side3 = 5.0;
        // finding the perimeter
        double perimeter=(side1+side2+side3);
        System.out.println("Perimeter of triangle = "+perimeter);
    }
}
Output:

Perimeter of triangle = 10.0

Method-2: Java Program to Find Perimeter of Triangle Using dynamic value

In this we have not taken 3 sides of the triangle as static values. Here, it will ask the user to take lengths of the 3 sides of the triangle as dynamic input. Then these lengths will be used to find the perimeter using the formula.

Let’s see the program to understand it more clearly.

import java.util.*;

class Main 
{
    // Driver method
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        //length of 3 sides of triangle will be taken by the user
        System.out.print("Enter length for side1 = ");
        double side1 = sc.nextDouble(); 
        System.out.print("Enter length for side2 = ");
        double side2 = sc.nextDouble(); 
        System.out.print("Enter length for side3 = ");
        double side3 = sc.nextDouble();
        // finding the perimeter
        double perimeter=(side1+side2+side3);
        System.out.println("Perimeter of triangle = "+perimeter);
    }
}
Output:

Enter length for side1 =2.2
Enter length for side1 =2.2
Enter length for side1 =2.2
Perimeter of triangle = 6.6000000000000005

Method-3: Java Program to Find Perimeter of Triangle Using user defined method

In this we have taken one user defined method which contains the logic to find the perimeter of triangle. The lengths of the 3 sides of the triangle are passed as parameter to the user defined mnethod i.e Perimeter() method.

Let’s see the program to understand it more clearly.

import java.util.*;

public class Main 
{
    static double Perimeter(double side1, double side2, double side3)
    {
        // Finding Perimeter of triangle
        // and returning the perimeter
        return (side1+side2+side3);
    }
 
    // Driver method
    public static void main(String[] args)
    {
        //lengths for 3sides of triangle declared
        double side1 = 2.2;
        double side2 = 3.0;
        double side3 = 2.2;
        System.out.println(Perimeter(side1, side2, side3));
    }
}
Output:

Perimeter of triangle = 7.4

Want to excel in java coding? Practice with these Java Programs examples with output and write any
kind of easy or difficult programs in the java language.

Related Java Programs: