» » » Java Example program to calculate area of reactangle

# Java Example program to calculate area of reactangle

In this tutorial we will see how to calculate Area of Rectangle.

1.Write a Basic java example  program to find area of Rectangle

•  To calculate the area of rectangle we need to ask user to enter length and width of the rectangle so that we can calculate area of rectangle using formula area of rectangle=length*width

1. package com.BasicJavaProgramsExamples;
2. import java.util.Scanner;
3. public Class AreaOfRectangle{
4.
5. public static void main(String args[]) {
6.
7. Scanner in= new Scanner(System.in);
8. System.out.println("Please enter length of a rectangle");
9.
10.     double length=in.nextDouble();
11.
12. System.out.println("Please enter width of a rectangle");
13.
14.     double width=in.nextDouble();
15.
16. /*
17.  *Area of a rectangle is
18.  *Area= length*width;
19.  *
20.  */
21.
22.     double area=length*width;
23.
24.     System.out.println("Area of the Rectangle="+area);
25.
26. }
27. }

Output:

1. Please enter length of a rectangle
2. 4
3. Please enter width of a rectangle
4. 8
5. Area of the circle =32.0

2.Write a Basic java example program to find Area of rectangle without user interaction

1. package com.BasicJavaProgramsExamples;
2. public Class AreaOfRectangle{
3.
4. public static void main(String args[]) {
5.
6.
7.   double length=4;
8.
9.   double width=8;
10.
11. /*
12.  *Area of a rectangle is
13.  *Area= length*width;
14.  *
15.  */
16.
17.     double area=length*width;
18.
19.     System.out.println("Area of the Rectangle="+area);
20.
21. }
22. }

Output:

1. Area of the circle =32.0
1.Write a Basic java example  program to find perimeter of Rectangle

•  To calculate the perimeter of rectangle we need to ask user to enter length and width of the rectangle so that we can calculate perimeter of rectangle using formula perimeter of rectangle=2*length*width

1. package com.BasicJavaProgramsExamples;
2. import java.util.Scanner;
3. public Class PerimeterOfRectangle{
4.
5. public static void main(String args[]) {
6.
7. Scanner in= new Scanner(System.in);
8. System.out.println("Please enter length of a rectangle");
9.
10.     double length=in.nextDouble();
11.
12. System.out.println("Please enter width of a rectangle");
13.
14.     double width=in.nextDouble();
15.
16. /*
17.  *perimeter of a rectangle is
18.  *perimeter= length*width;
19.  *
20.  */
21.
22.     double perimeter=length*width;
23.
24.     System.out.println("Perimeter of the Rectangle="+perimeter);
25.
26. }
27. }

Output:

1. Please enter length of a rectangle
2. 4
3. Please enter width of a rectangle
4. 8
5. Perimeter of the circle =64.0