Triangle class java code: 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 or perimeter of triangle java program, perimeter of triangle in java, perimeter of triangle program in java, triangle perimeter java, area of triangle in java, triangle java code, java program to find area and perimeter of triangle, area of triangle with 3 sides in java, area of triangle java program, area and perimeter of triangle in java, area of triangle program in java discussed below.
Program to Find Perimeter of Triangle
Triangle program in java: 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.
Recommended Reading On: Python Program to Calculate the Area, Semi Perimeter and Perimeter of a Triangle
Answer this:
- Write a program to find the perimeter of a triangle in java?
- How to find the perimeter of a triangle?
- Find the perimeter of a triangle?
- Write a program to calculate the perimeter of a triangle having sides of length 2 3 and 5 units java?
- How to find perimeter of a triangle?
- Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating a class named ‘triangle’ with constructor having the three sides as its parameters?
- How to solve perimeter of triangle?
- How to get the perimeter of a triangle?
- Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating a class named ‘triangle’ without any?
- Parameter in its constructor?
- How to compute perimeter of triangle?
- How to find perimeter of triangle?
Related Java Programs: