# How to calculate Area and Perimeter of Square through a simple Java program ?

## Calculation of Area and Perimeter of Square through a Java program

Hello friends,

In this hub, I will be teaching you, how to calculate Area and Perimeter of Square. In this hub, I will present a simple Java program, which will calculate Area and Perimeter of Square. The program will start by prompting the user to enter length of side of square. All the sides of Square are equal in length. Output of the Java program will be displayed on the console. The source code will demonstrate working of Scanner class method such as "nextInt". This method is very important, when it comes to read values from console. It reads user input in the form of integer values.

The formula for the calculation of Area of Square is as follows :

**Area of Square = side * side, where length of side will be provided by the user.**

The formula for calculation of Perimeter of Square is as follows :

**Perimeter of Square = 4 * side , where length of side will be provided by the user.**

The source code is provided below. The source code calculates Area and Perimeter of Square and displays it on the console. All the source code is written in the main method, which is the starting point for execution of Java program. Java Virtual Machine always searches for main method with signature as - **public static void main ( String [ ] args) { }** .

Below is the image, which provides the formula for calculation of Area and Perimeter of Square.

Below is the video tutorial which demonstrates, how to calculate Area and Perimeter of Square in Java using Scanner class.This video tutorial has been created for providing better understanding over the topic of mensuration.

## Area and Perimeter of Square video tutorial

## Steps to implement

Step 1 - Create a Scanner object. Pass "System.in" object as parameter to its constructor. "System.in" object helps scanner class to read user inputs from the console.

Step 2 - Prompt the user to enter the length of side of square.

Step 3 - Use the Scanner class, nextInt method to read the length of side of square, which user enters on the console. The user enters length of side of square, which is of data type integer. After providing value 5, user has to hit enter key. This value is been read by the method "nextInt".

Step 4 - In order to calculate area of square, use the formula as : Area of Square = side * side.

Step 5 - In order to calculate perimeter of square, use the formula as : Perimeter of Square = 4 * side.

Step 6 - Store the area and perimeter of square in respective variables. Finally, print the area and perimeter of square on the console.

import java.util.Scanner; public class Square { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter length of side of Square : "); int side = scanner.nextInt(); double area = 0; double perimeter = 0; area = side * side; perimeter = 4 * (side); System.out.println("Area of Square is : " + area); System.out.println("Perimeter of Square is : " + perimeter); } }

The image below is the snapshot of Java console in Eclipse IDE. Upon running the Java program in eclipse, it prompts the user to enter length of side of Square. Here, in our case user enters length of square as 5 and than hit enter key. After calculation of Area and Perimeter of Square, it displays area and perimeter of square on the Java console.

## Output of the program -

## Which is best programming language ?.

## Watch my complete Youtube channel below -

- Java Hubberspot - YouTube

Hello friends, I am Dinesh Varyani. Owner of blog http://www.hubberspot.com . This channel will have Java Programming Tutorials for beginners ... Visit my Ja...

## Comments

No comments yet.