beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java program to calculate area of Square

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

Enjoyed this post? Try these related posts

  1. Java Program to reverse the Array
  2. Java program to check prime number
  3. Java program to get IP address
  4. Java Program to Calculate Area of Rectangle
  5. Java program to print Floyd’s triangle – Example
  6. Java Program to break Integer into Digits

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 *

Programs

  • C Programs
  • Java Programs

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

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap