Find the Area of a Square in Java



Given a square whose length of all its sides are l, write a Java program to find its area. A square is a rectangle whose length and breadth are same. Therefore, area of this type of rectangle is the square of its length.

To calculate the area of square in Java, you simply need to multiply the given length of square with the length itself and store the result in another variable.


Java Program to Find The Area of a Square

A Java program that illustrates how to find the area of the square is shown below ?

public class AreaOfSquare {
   public static void main(String args[]){
      int length, area;
      // length of the square
      length = 55;
      System.out.println("Length of the square:: " + length);
      // calculating area 
      area = length * length;
      // printing the result
      System.out.println("Area of the square is:: " + area);
   }
}

Output

Length of the square:: 55
Area of the square is:: 3025

Java Function to Find The Area of a Square

In this example, we are calculating the area of a square with the help of a user-defined function.

public class AreaOfSquare {
   // defining a method to calculate area 
   public static void calcAr(int l) {
      int area = l * l;
      System.out.println("Area of the square is:: " + area);    
   } 
   public static void main(String args[]){
      int length = 33;
      System.out.println("Length of the square:: " + length);
      // calling the method
      calcAr(length);
   }
}

Output

Length of the square:: 33
Area of the square is:: 1089
Updated on: 2024-09-11T11:05:11+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements