A Program to Find the Area of a Square

In this example, we are giving a program to find the area of a square. The rule of finding the area of a square is: A = length * length.


Program to find the area of a square:

  1. import java.util.Scanner; //Scanner class helps to get input from user
  2. class triangle {
  3. public static void main(String[] args) {
  4. Scanner s = new Scanner(System.in); //Scanner class object declaration
  5. System.out.println("Enter the length of a side: ");
  6. float l = s.nextFloat(); // takes float input length from keyboard or user or run time
  7. float area = l * l;
  8. System.out.println("Area of Square: "+ area);
  9. }
  10. }

Output:

Enter the length of a side: 3
Area of Square: 9.00