Java Program to calculate the area of a square

Here we are explaining how to calculate the area of a square.

 How to calculate the area of a square?

A square has four equal sides. The area of a square is calculated by using the formula area=a*a.  where a is the length of the square.

How to implement the java program to find out the area of a square?

First, we have to declare the class AreaSquare.Create an object of the scanner class as sc and read the length of the square from the user into the variable s. Calculate the area of the square as a=s*s. Then display area as a.

ALGORITHM

STEP 1: Declare the class AreaSquare with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Read the side of the square from the user into the variable s.

STEP 4: Calculate the area of the square as a=s*s.

STEP 5: Display the area of the square a using System.out.println() method.

 

Java Source Code

                                          import java.util.Scanner;
public class AreaSquare{
   public static void main(String args[]) {   
       double a,s;
      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the side of the square:");
      s = sc.nextDouble();

      a = s*s;
      System.out.println("Area of the square is: " + a);    
   }
}
                                      

OUTPUT

Enter the side of the square:10
Area of the square is: 100.0