Java Program to Calculate Area of Square

In this tutorial, we will learn how to write a Java Program to calculate the Area of Square.
We will read input from the console using Scanner class.

Java Program to Calculate Area of Square

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to Calculate Area of Square
 * 
 * @author https://www.sourcecodeexamples.net/
 *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("Enter Side of Square:");
            // Storing the captured value in a variable
            double side = scanner.nextDouble();
            // Area of Square = side*side
            double area = side * side;
            System.out.println("Area of Square is: " + area);
        }
    }
}
Output:
Enter Side of Square:
10
Area of Square is: 100.0

Related Java Programs


Comments