Java Program to Find the Perimeter of a Circle
In this tutorial, we will learn how to calculate the perimeter of circle, rectangle, and triangle in java. The perimeter of a shape is defined as a path that encompasses/surrounds/outlines a shape. A circle is a shape consisting of all points in a plane that are at a given distance from a given point, the center. A rectangle is defined as a quadrilateral with four right angles. A triangle is defined as a polygon with three edges and three vertices. But before moving forward if you are not familiar with the concept of data types, then do check the article on Data types in Java.
Input: Enter the radius of the circle: 5
Enter the length of the rectangle: 4
Enter the breadth of the rectangle: 5
Enter the sides of the triangle: 3
Enter the sides of the triangle: 4
Enter the sides of the triangle: 5
Output:
The area of the Circle: 31.4
The area of the Rectangle: 18.0
The area of the Triangle: 12.0
Two Cases arise for the above situation:
Case 1: When the sides and radius are given
Case 2: When the area is given
Let us look at each of these cases separately.
Program 1: Find the Perimeter of Circle, Rectangle, and Triangle
In this program, we will see how to calculate the perimeter of the circle, rectangle, and triangle in java when the sides and radius are given.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare variables to store the value of the sides and radius of the rectangle, triangle, and circle.
- Ask the user to initialize the variables.
- Declare another variable to store the perimeter.
- Create an object of the Main class.
- Create three user-defined methods to calculate the perimeter of the rectangle, triangle, and circle.
- Use the formula to calculate the perimeter for each of them.
- Display the result.
- Stop
The below program demonstrates how to calculate the perimeter of the circle, rectangle, and triangle in java.
//Java Program to Calculate the Perimeter of a Circle, Rectangle, and Triangle
import java.util.Scanner;
public class Main
{
double pi = 3.14,perimeter;
Scanner s = new Scanner(System.in);
void circle()
{
System.out.println("Enter the radius of circle: ");
int radius = s.nextInt();
perimeter = 2 * pi * radius;
System.out.println("The perimeter of the circle: "+perimeter);
}
void rectangle()
{
System.out.println("Enter length of rectangle: ");
int length = s.nextInt();
System.out.println("Enter breadth of rectangle: ");
int breadth = s.nextInt();
perimeter = 2 * (length + breadth);
System.out.println("The perimeter of the rectangle: "+perimeter);
}
void triangle()
{
System.out.println("Enter the length of the first side of triangle: ");
int side1 = s.nextInt();
System.out.println("Enter the length of the second side of triangle: ");
int side2 = s.nextInt();
System.out.println("Enter the length of the third side of triangle: ");
int side3 = s.nextInt();
perimeter = side1 + side2 + side3;
System.out.println("The perimeter of the triangle: "+perimeter);
}
public static void main(String []args)
{
Main obj=new Main();
obj.circle();
obj.rectangle();
obj.triangle();
}
}
Enter radius of circle: 5
The perimeter of the circle: 31.400000000000002
Enter the length of rectangle: 2
Enter breadth of rectangle: 3
The perimeter of rectangle: 10.0
Enter the length of the first side of the triangle: 6
Enter the length of the second side of the triangle: 7
Enter the length of the third side of the triangle: 8
The perimeter of the triangle: 21.0
Program 2: Find the Perimeter of Circle, Rectangle, and Triangle
In this program, we will see how to calculate the perimeter of the circle, rectangle, and triangle in java when the area is given.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare variables to store the value of the area of the rectangle, triangle, and circle.
- Ask the user to initialize the variables.
- Declare other variables to store the other parameters of the rectangle and triangle.
- Create an object of the Main class.
- Create three user-defined methods to calculate the perimeter of the rectangle, triangle, and circle.
- Use the formula to calculate the perimeter for each of them.
- Display the result.
- Stop
The below program demonstrates how to calculate the perimeter of the circle, rectangle, and triangle in java.
//Java Program to Calculate the Perimeter of a Circle, Rectangle, and Triangle
import java.util.Scanner;
public class Main
{
double pi = 3.14,perimeter;
Scanner s = new Scanner(System.in);
void circle()
{
System.out.println("Enter the area of the circle: ");
double area = s.nextDouble();
perimeter = 2*(Math.sqrt(pi*area));
System.out.println("The perimeter of the circle: "+perimeter);
}
void rectangle()
{
System.out.println("Enter the length of the rectangle: ");
double length = s.nextDouble();
System.out.println("Enter the area of the rectangle: ");
double area = s.nextDouble();
double res=2*(area/length);
perimeter =res+(2*length);
System.out.println("The perimeter of the rectangle: "+perimeter);
}
void triangle()
{
System.out.println("Enter the length of the first side of triangle: ");
double side1 = s.nextDouble();
System.out.println("Enter the length of the second side of triangle: ");
double side2 = s.nextDouble();
System.out.println("Enter the length of the height of the triangle: ");
double h = s.nextDouble();
System.out.println("Enter the area of the triangle: ");
double area = s.nextDouble();
if(h<=side1 && h<=side2) {
double res=2*(area/h);
perimeter = side1 + side2 + res;
System.out.println("The perimeter of the triangle: "+perimeter);
}
else
{
System.out.println("Enter the correct value of h");
}
}
public static void main(String []args)
{
Main obj=new Main();
obj.circle();
obj.rectangle();
obj.triangle();
}
}
Enter the area of the circle: 50
The perimeter of the circle: 25.059928172283335
Enter the length of the rectangle: 2
Enter the area of the rectangle: 5
The perimeter of rectangle: 9.0
Enter the length of the first side of the triangle: 6
Enter the length of the second side of the triangle: 7
Enter the length of the height of the triangle: 5
Enter the area of the triangle: 9
The perimeter of the triangle: 16.6