Java program to find perimeter of square

Let’s learn java program to find perimeter of square.

Java program to find perimeter of square

Find perimeter of square is one of the common java interview question.

In the below java program first user enters side of square using nextInt() method of Scanner class.

Also read – java program to find area of rectangle

This value is stored in integer variable ‘side’. In the next step perimeter is calculated using below formula.

perimeter = 4 * side

Finally perimeter of a square is printed on console. Let’s see java program

import java.util.Scanner;
public class SquarePerimeter
{
   public static void main(String[] args)
   {
      int side, perimeter;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter side length of square: ");
      side = sc.nextInt();
      perimeter = 4 * side;
      System.out.print("Perimeter of square is: " + perimeter);
      sc.close();
   }
}


Output:

Please enter side length of square: 6
Perimeter of square is: 24


Now let’s learn to calculate area of square. Here’s the java program to calculate area of square.

import java.util.Scanner;
public class FindAreaOfSquare
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter side of square: ");
      double side = sc.nextDouble();
      // formula area of square = side * side
      double area = side * side;
      System.out.println("Area of square is: " + area);
      sc.close();
   }
}


Output:

Please enter side of square: 5
Area of square is: 25.0


Also read – program to find area of rectangle using inheritance in java