Hello coders, in this post, we will learn Java program to calculate area and perimeter of Rectangle. In the previous tutorial, you have learned Java Program to calculate Area and Perimeter of Right Triangle.
Let’s see the mathematical formula to find the area and perimeter of the rectangle.
Area of rectangle = length * breadth
Perimeter of rectangle = 2(length+ breadth)
Now let’s start to write the program. So the complete source code is given below.
Java Program to Calculate Area and Perimeter of Rectangle
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 |
import java.util.*; public class Rectangle { public static void main(String[] args) { double length, breadth, area, perimeter; Scanner in = new Scanner(System.in); // Asking the user to enter the length and breadth of the rectangle System.out.println(" Enter the length and breadth of the rectangle :"); length = in.nextDouble(); breadth = in.nextDouble(); area = length * breadth; // Calculating area of rectangle perimeter = 2 * (length + breadth); // Calculating perimeter of rectangle System.out.println(" Area of Rectangle : " + area +" square units); System.out.println(" Perimeter of Rectangle : " + perimeter); } } |
What we Did?
- First of all we have imported the util package.
- Then created a class and named it Rectangle.
- After that started main() method of the program.
- Then inside main() method declared variables.
- Then created object of the Scanner class.
- Now asked the user to enter the length and breadth of the rectangle. Then read the value of length and breadth entered by the user.
- Then calculated area of the rectangle using the formula area = length * breadth
- In the next line calculated perimeter of the rectangle using the formula perimeter = 2 (length + breadth)
- Then finally displayed the value of the area and perimeter of the rectangle on the screen.
Output
1 2 3 4 |
Enter the length and breadth of the rectangle : 4 2 Area of Rectangle : 8.0 square units Perimeter of Rectangle : 12.0 |
So guys, in this tutorial, we have learned Java Program to calculate Area and Perimeter of Rectangle. In the next tutorial, you will learn Java Program to calculate Area and Perimeter of Square.
Related Tutorials…
- Java Program to Calculate Area and Circumference of Circle
- Java Program to calculate Area of Triangle
- Java Program to calculate Area of Triangle using heron’s formula
- Java Program to calculate Area and Perimeter of Right Triangle
- Java Prorgram to calculate Area and Perimeter of Parallelogram
- Java Program to calculate Area of Trapezium
- Java Program to calculate Area and Volume of Cube
- Java Program to calculate Area and Volume of Cuboid
- Java Program to calculate Total Surface Area and Curved Surface Area of cylinder
- Java Program to calculate Volume of Cone
- Java Program to calculate Surface Area and Volume of Sphere