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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.*; public class Square { public static void main(String[] args) { double side, area, perimeter; Scanner in = new Scanner(System.in); // Asking the user to enter the side of square System.out.println(" Enter the side of the square :"); side = in.nextDouble(); area = side * side; // Calculating area of square perimeter = 4 * side; // Calculating perimeter of square System.out.println(" Area of Square : " + area +" square units"); System.out.println("\nPerimeter of Square : " + perimeter); } } |
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
1 2 3 |
Enter the side of the square :3 Area of Square : 9.0 square units Perimeter of Square : 12.0 |
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…
- Java Program to Calculate Area and Circumference of Circle
- Java Program to calculate Area of Triangle
- Java Program to calculate Area of Triangle using heron’s formula
- Java Program to calculate Area and Perimeter of Right Triangle
- Java Program to calculate Area and Perimeter of Rectangle
- Java Program to calculate Area of Trapezium
- Java Program to calculate Area and Volume of Cube
- Java Program to calculate Area and Volume of Cuboid
- Java Program to calculate Total Surface Area and Curved Surface Area of cylinder
- Java Program to calculate Volume of Cone
- Java Program to calculate Surface Area and Volume of Sphere