Find area & perimeter of a square in java (with example)

What is Square shape ?

The square is a fundamental shape in geometry, characterized by several defining properties. It is a polygon with four equal sides and four right angles (90-degree angles) at each corner.

Key features of a square shape:

  • All sides of a square have the same length.
  • Each corner or vertex of a square measures 90 degrees, making it a regular quadrilateral.
  • Due to its equal sides and angles:
    • a square possesses symmetry along its diagonals and axes.
  • The diagonals of a square bisect each other at right angles and are of equal length.
Area & Perimeter of Square in Java
FIg 1: Square Shape

How to find area & perimeter of square?

What is area of square?

  • Suppose S is side of square
  • Area: A = S * S
    • i.e. Area A = S²(where S represents the side length)
  • if side of Square is 5, then area of square is:
      • Area = 5 * 5
      • Area = 25
  • if side of Square is 20, then area of square is:
      • Area = 20 * 20
      • Area = 400

What is area of square?

  • Suppose S is side of square
  • Perimeter: P = 4 * S, where S represents the side of square.
  • if side of Square is 5, then Perimeter of square is:
      • Perimeter = 4 * 5
      • Area = 20
  • if side of Square is 20, then perimeter of square is:
      • Perimeter = 4 * 20
      • Area = 80
area perimeter square
Fig 2: Area & Perimeter of square

What are applications of Square shape?

Squares find extensive use in various fields such as mathematics, architecture, art, and engineering due to their symmetry and predictable properties. They serve as foundational shapes in many geometric concepts and constructions.

What is approach to find area & perimeter of square?

  • Get the Side of square from User.
    • We will use Java Scanner class to get side of square.
  • Area of a square is by side of square * side of square.
  • Perimeter of a square is 4 * side of square
  • Apply above formulas to calculate area * perimeter of square.
  • Print the values to console as output.

Program: calculate area & perimeter of a square in java

package org.learn;

import java.util.Scanner;

public class AreaAndPerimeterOfSquare {

 public static void main(String[] args) {
  try (Scanner scanner = new Scanner(System.in)) {
   System.out.printf("1. Enter side of square : ");
   double side = scanner.nextDouble();   
    
   //Area of square is side * side
   double area = side * side;
   //Print area up to two precision
   System.out.printf("2. Area of square is : %4.2f",area);
   
   //Area of square is side * side
   double Perimeter = 4 * side;
   //Print area up to two precision
   System.out.printf("\n3. Perimeter of square is : %4.2f",Perimeter);
  }
 }
}

Output: area &  perimeter of a square in java

1. Enter side of square : 5
2. Area of square is : 25.00
3. Perimeter of square is : 20.00

1. Enter side of square : 7
2. Area of square is : 49.00
3. Perimeter of square is : 28.00
Scroll to Top