Java Program to Calculate Area and Perimeter of Square

Hello everyone, today we will learn Java Program to calculate Area and Perimeter of Square. In the previous tutorial, we learned Java Program to calculate Area and Perimeter of Rectangle. Calculating the area and perimeter of the square in Java is very easy. Let’s see how to do it.

We can find the area and perimeter of the square using the following formula.
 
Area of a Square = side × side = side² 

Perimeter of a square = 4 x side 

Now let’s write the Program, and the explanations are given after the Program.

Java Program to calculate Area and Perimeter of Square

What we did?

  • First of all we have imported the util package.
  • Then created a class and named it Square.
  • After that started main() method of the program.
  • Then inside main() method declared variables.
  • Then created object of the Scanner class.
  • Now asked the user to enter the side of square. Then read the value of side of square entered by the user.
  • Then calculated area of square using the formula area = side * side
  • In the next line calculated perimeter of square using the formula perimeter = 4 * side
  • Then finally displayed the value of area and perimeter of square on the screen.

Output

So in this tutorial, we have learned Java Program to calculate Area and Perimeter of a Square. In the next tutorial, we will learn Java Program to calculate Area and Perimeter of Parallelogram.


Related Tutorials…

Leave a Comment