BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java program to calculate area of Square

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial we will learn how to calculate area of Square. Following are the two ways to do it:

1) Program 1: Prompting user for entering the side of the square
2) Program 2: Side of the square is specified in the program’ s source code.

Program 1:

/**
 * @author: BeginnersBook.com
 * @description: Program to Calculate Area of square.Program 
 * will prompt user for entering the side of the square.
 */
import java.util.Scanner;
class SquareAreaDemo {
   public static void main (String[] args)
   {
       System.out.println("Enter Side of Square:");
       //Capture the user's input
       Scanner scanner = new Scanner(System.in);
       //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:
2.5
Area of Square is: 6.25

Program 2:

/**
 * @author: BeginnersBook.com
 * @description: Program to Calculate Area of square.
 * No user interaction: Side of square is hard-coded in the
 * program itself.
 */
class SquareAreaDemo2 {
   public static void main (String[] args)
   {
       //Value specified in the program itself
       double side = 4.5;
       //Area of Square = side*side
       double area = side*side; 
       System.out.println("Area of Square is: "+area);
   }
}

Output:

Area of Square is: 20.25

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Automorphic Number Program in Java
  3. Java Program to Print Sandglass Star Pattern
  4. Peterson Number Program in Java
  5. Java Program to break Integer into Digits

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. william says

    February 10, 2016 at 1:06 AM

    please keep on sending me any new update in java programming

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap