Java program to enter length and breadth of a rectangle and find its perimeter
ADVERTISEMENTS
In this article, you will learn how to find the perimeter of a rectangle in the Java language.
You should have knowledge of the following topics in java programming.
- Java
java.util.Scanner
package - Java
main()
method - Java
System.out.println()
method
Formula
This is the standard formula to calculate the perimeter of a rectangle.
P = 2(l + w)
Where
P
= perimeter
l
= length of the rectangle
w
= width of the rectangle
Source Code
// Java program to find the perimeter of a rectangle
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the length & width of the rectangle::\n");
// Reading data using readLine
float l = in.nextFloat();
float w = in.nextFloat();
float p;
/* Calculate perimeter of rectangle */
p = 2 * (l + w);
System.out.print("\n");
System.out.println("The Perimeter of rectangle = " + p + " units");
}
}
Output
Enter the length & width of the rectangle::
5
7
The Perimeter of rectangle = 24.0 units
Explanation
In this program, we have taken two inputs from the user rectangle's width 5
and rectangle's height 7
.
Then used the standard formula P = 2(l + w)
to calculate the perimeter of a rectangle then it will return the 24.0 units
the perimeter of the rectangle.