# Calculating Perimeter and Area of a Rectangle

This tutorial shows how to calculate the area and perimeter of a rectangle (or square) by 3 different methods. In all solutions, length and width are entered by the user.

Solution 1: The shortest solution for calculating perimeter and area of a rectangle using user input for width and length.

``````Scanner input = new Scanner(System.in);
System.out.println("What is length of the Rectangle?");
int a = input.nextInt();

System.out.println("What is width of the Rectangle?");
int b = input.nextInt();

int perimeter = 2 * (a + b);
int area = a * b;

System.out.println("Area of Rectangle is: " + area);
System.out.println("Perimeter of Rectangle is: " + perimeter);``````

Console Output:

What is length of the Rectangle?
4
What is width of the Rectangle?
7
Area of Rectangle is: 28
Perimeter of Rectangle is: 22

Solution 2: Calculating area and perimeter of a rectangle with a fancy output screen.

``````System.out.println("\t\tb");
System.out.println("");
System.out.println("\t****************");
System.out.println("\t*              *");
System.out.println("\t*              *");
System.out.println("a\t*              *");
System.out.println("\t*              *");
System.out.println("\t*              *");
System.out.println("\t****************");

System.out.println("");
System.out.println("Welcome to perimater and area calculator");

Scanner input = new Scanner(System.in);
System.out.print("Please enter a number for a side > ");
int a = input.nextInt();

System.out.print("Please enter a number for b side > ");
int b = input.nextInt();

int perimeter = 2 * (a + b);
int area = a * b;

System.out.println("Area of Rectangle is: " + area);
System.out.println("Perimeter of Rectangle is: " + perimeter);``````

Output Screen: Solution 3: Calculating area and perimeter of a rectangle with seperate Java methods.

``````public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a number for width > ");
int width = input.nextInt();

System.out.print("Please enter a number for length > ");
int length = input.nextInt();

int perimeter = calculatePerimeter(width,length);
int area = calculateArea(width,length);

System.out.println("Area of Rectangle is: " + area);
System.out.println("Perimeter of Rectangle is: " + perimeter);
}

public static int calculatePerimeter (int a, int b)
{
int perimeter = 2 * (a + b);
return perimeter;
}

public static int calculateArea (int a, int b)
{
int area = a * b;
return area;
}``````

Console Output:

Please enter a number for width > 3
Please enter a number for length > 8
Area of Rectangle is: 24
Perimeter of Rectangle is: 22