To find the area of a rectangle or a square you need to multiply the length and the width of a rectangle or a square. There are different units for perimeter and area. The perimeter has the same units as the length of the sides of rectangle or square whereas the area’s unit is squared.
In this example. I’ll show how to calculate Area and Perimeter of Rectangle in Java Program.
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 |
package javaexamples; import java.util.Scanner; public class JavaExamples { public static void main(String[] args) { /*Calculate Area and Perimeter of Rectangle */ Scanner in = new Scanner(System.in); int b1, b2, area, perimeter; System.out.println("Width: "); b1 = in.nextInt(); System.out.println("Height: "); b2 = in.nextInt(); area = b1 * b2; perimeter = 2 * (b1 + b2); System.out.println("area="+area); System.out.println("perimeter=" + perimeter); } } |
Output: