Java Program to Find Area and Perimeter of Square

In the previous article we have discussed Java Program to Find Area and Perimeter of Rectangle

In this article we will discuss about how to find area and perimeter of a square.

Program to Find Area and Perimeter of Square

Before going into the program, let’s see how we find the area and perimeter of a square.

Formula for area of Square = side*side

Formula for perimeter of Square = 4*side

Where,

  •  side*side represents the length*breadth of the square. (each side of square is same)
  • 4*side represents the 4*length of side of the square. (each side of square is same)

Example:

Example- To find Area of Square

When length of each side of square = 4
Then area of square => area = side*side
                                 => area = 4*4
                                 => area = 16
Example- To find Perimeter of Square

When length of each side of square = 4
Then area of square => perimeter = 4*side
                                 => perimeter = 4*4
                                 => perimeter =16

Now, let’s see the program.

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Method-1: Java Program to Find Area and Perimeter of Square By Using Static Values

In this method, the side length of square is already declared in the program and stored by the variable sideLength. Which will be used to calculate the area and perimeter of square by using the formula.

import java.util.*;

public class JavaProgram
{
    public static void main(String args[])
    {
        int sideLength;
        // length of each side of square is given
        sideLength = 5;
        //finding area of square
        int area = sideLength*sideLength;
        //finding perimeter of square
        int perimeter = 4*sideLength;
        
        System.out.print("Area of Square = " +area);
        
        System.out.print("\nPerimeter of Square = " +perimeter);
    }
   
}
Output:

Area of Square = 25
Perimeter of Square = 20

Method-2: Java Program to Find Area and Perimeter of Square By Using Dynamic Values

In this method, the side length of square will be taken as user input and then the area and perimeter of square will be calculated as per method-1. Method-1 and Method-2 is same the difference is only in assigning side length value of square.

import java.util.*;

public class JavaProgram
{
    public static void main(String args[])
    {
        int side, perimeter, area;
        //Scanner class object created fro taking user input
        Scanner sc = new Scanner(System.in);
        //taking the length of any side of square from the user
        System.out.print("Enter length of any side of square : ");
        side = sc.nextInt();
        
        //finding area of square
        area = side*side;
        //finding perimeter of square
        perimeter = 4*side;
        
        System.out.print("Area of Square = " +area);
        
        System.out.print("\nPerimeter of Square = " +perimeter);
    }
}
Output:

Enter length of any side of square : 4
Area of Square = 16
Perimeter of Square = 16

Method-3: Java Program to Find Area and Perimeter of Square By Using User Defined Method

In this method, first the side length of square will be taken by the user and this value will be passed to the user defined method calculate() where the logic to find area and perimeter is implemented.

import java.util.*;

public class JavaProgram
{
    public static void main(String args[])
    {
        int side;
        //Scanner class object created fro taking user input
        Scanner sc = new Scanner(System.in);
        //taking the length of any side of square from the user
        System.out.print("Enter length of any side of square : ");
        side = sc.nextInt();
        calculate(side);
    }
    
    public static void calculate(int sideLength)
    {
        //finding area of square
        int area = sideLength*sideLength;
        //finding perimeter of square
        int perimeter = 4*sideLength;
        
        System.out.print("Area of Square = " +area);
        
        System.out.print("\nPerimeter of Square = " +perimeter);
    }
   
}
Output:

Enter length of any side of square : 4
Area of Square = 16
Perimeter of Square = 16

Explore complete java concepts from the Java programming examples and get ready to become a
good programmer and crack the java software developer interview with ease.

Related Java Programs: