In this article we will show you, How to write Java Program to find Area Of Rectangle and Perimeter of Rectangle with example. Before we step into the Java program, Let see the definitions and formulas behind Perimeter and Area Of Rectangle.

### Area of a Rectangle

- If we know the width and height of a rectangle, we can calculate the area of a rectangle using the formula: Area = Width * Height
- Perimeter is the distance around the edges. We can calculate perimeter of a rectangle using the formula: Perimeter = 2 * (Width + Height)

## Java Program to find Area of Rectangle & Perimeter of Rectangle

This Java program allows the user to enter width and height of the rectangle. Using those values we will calculate the Area of a rectangle and perimeter of a rectangle.

**JAVA CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Java Program to find Area of Rectangle and Perimeter of Rectangle package Area; import java.util.Scanner; public class AreaOfRctangle { private static Scanner sc; public static void main(String[] args) { double width, height, Area, Perimeter; sc = new Scanner(System.in); System.out.println(" Please Enter the Width of a Rectangle = "); width = sc.nextDouble(); System.out.println(" Please Enter the Height of a Rectangle = "); height = sc.nextDouble(); Area = width * height; Perimeter = 2 * (width + height); System.out.format(" The Area of a Rectangle = %.2f\n",Area); System.out.format(" The Perimeter of a Rectangle = %.2f\n", Perimeter); } } |

**OUTPUT**

**ANALYSIS**

Following statements will allow the User to enter the Width and Height of a rectangle and then we are going to assign those values to already declared variables called Width and Height.

**TIP:** You can remove the second System.out.println statement to shorten the code.

1 2 3 4 5 |
System.out.println(" Please Enter the Width of a Rectangle = "); width = sc.nextDouble(); System.out.println(" Please Enter the Height of a Rectangle = "); height = sc.nextDouble(); |

Next, we are using the Mathematical Formula to calculate the area of rectangle

1 |
Area = width * height; |

In the next line, We are calculating the Perimeter of the rectangle

1 |
Perimeter = 2 * (width + height); |

Following System.out.format statements will help us to print the Perimeter and Area of a rectangle

1 2 |
System.out.format(" The Area of a Rectangle = %.2f\n",Area); System.out.format(" The Perimeter of a Rectangle = %.2f\n", Perimeter); |

## Java Program to find Area of Rectangle using Functions

This Java program allows the user to enter width and height of the rectangle. In this example, we are going to use the logic that we specified in first example but we will separated the logic and place it in a method.

**JAVA CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// Java Program to find Area of Rectangle using Functions package Area; import java.util.Scanner; public class AreaOfRctangleUsingMethods { private static Scanner sc; public static void main(String[] args) { double width, height; sc = new Scanner(System.in); System.out.println("\n Please Enter the Width of a Rectangle = "); width = sc.nextDouble(); System.out.println("\n Please Enter the Height of a Rectangle = "); height = sc.nextDouble(); AreaofRectangle(width, height); } public static void AreaofRectangle( double width, double height ) { double Area, Perimeter; Area = width * height; Perimeter = 2 * (width + height); System.out.format("\n The Area of a Rectangle = %.2f\n",Area); System.out.format("\n The Perimeter of a Rectangle = %.2f\n", Perimeter); } } |

**OUTPUT**

## Java Program to find Area of Rectangle using Oops

In this example, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a methods.

**TIP:** In general, You don’t have to write first method. We used this method to show the available options

**JAVA CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package Area; public class AreaOfaRectangle { double Area, Perimeter; public void AreaofRectangle( double width, double height ) { Area = width * height; Perimeter = 2 * (width + height); System.out.format(" The Area of Rectangle = %.2f\n", Area); System.out.format(" The Perimeter of Rectangle = %.2f\n", Perimeter); } public double RectangleArea( double width, double height ) { Area = width * height; return Area; } } |

Within the Main program, we will create an instance of the above specified class and call the methods.

**JAVA CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package Area; import java.util.Scanner; public class AreaOfRectangleUsingClass { private static Scanner sc; public static void main(String[] args) { double width, height, Area; sc = new Scanner(System.in); System.out.println(" Please Enter the Width of a Rectangle = "); width = sc.nextDouble(); System.out.println(" Please Enter the Height of a Rectangle = "); height = sc.nextDouble(); AreaOfaRectangle ar = new AreaOfaRectangle(); ar.AreaofRectangle(width, height); Area = ar.RectangleArea(width, height); System.out.format("\n Second Method: The Area of Rectangle = %.2f ", Area); } } |

**OUTPUT**

**ANALYSIS**

**AreaOfARectangle Class Analysis:**

- First we declared a function
**AreaofRectangle**with two arguments. Within the function, we are calculating the Area and Perimeter of Rectangle using respective mathematical formulas. Within the function, we used the**System.out.println**statement to print the output. - Next we declared a
**RectangleArea**function with two arguments of type double. Within the function, calculating the Area of Rectangle and we are returning the Value.

**TIP:** If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

**Main Class Analysis:**

First we created an instance / created an Object of the **AreaOfARectangle** Class

1 |
AreaOfaRectangle ar = new AreaOfaRectangle(); |

Next, we are calling the **AreaofRectangle** method. Note, this is the first method that we created with void keyword and this method will calculate Area and Perimeter and print the output from the **AreaOfARectangle** Class itself.

1 |
ar.AreaofRectangle(width, height); |

Next, we are calling the **RectangleArea** method. This is the second method that we created with double data type and this method will calculate Area and return a value so, we are assigning the return value to Area variable.

1 |
Area = ar.RectangleArea(width, height); |

Lastly we used following System.out.format statement to print the Area of a rectangle which is calculated by **RectangleArea** method

1 |
System.out.format("\n Second Method: The Area of Rectangle = %.2f ", Area); |

Thank You for Visiting Our Blog

## Share your Feedback, or Code!!