Java Program to find perimeter of square

In this program let us create a Java program to calculate and print the perimeter of square.


import java.util.Scanner;
public class PerimeterofSquare {
    public static void main(String args[]) {
        Scanner sc = new Scanner(;
        System.out.print("Enter the value of side:\t");
        int side = sc.nextInt();
        int perimeter = 4 * side;
        System.out.println("Perimeter of square is:\t" + perimeter);

Let us understand the program by breaking into simple parts:

  1. public class PerimeterofSquare: Any Java program starts with a class, in this case the class name is PerimeterofSquare
  2. public static void main(String[] args): main is the entry point for any Java program.
  3. Scanner sc = new Scanner(; : is passed as a parameter in Scanner class. This creates an object of Scanner class which is defined in java. util. scanner package. Scanner class tells the java compiler that system input will be provided through console(keyboard). corresponds to keyboard input or another input source specified by the host environment or user.
  4. int side = sc.nextInt(); : The nextInt() method of Java Scanner class is used to scan the next token of the input as an int. The value of side entered by the user is stored in the variable side.
  5. int perimeter = 4 * side; : Calculate the perimeter of square using the value of side given by the user.
  6. System.out.println("Perimeter of square is:\t" + perimeter); : System.out.println prints the perimeter of square with user entered inputs.


Enter the value of side:	7
Perimeter of square is:	28