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 Check if given Number is Perfect Square

Last Updated: February 23, 2019 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a java program to check if a given number is perfect square.

Java Example to check if a number is perfect square

In this program, we have created a user-defined method checkPerfectSquare() that takes a number as an argument and returns true if the number is perfect square else it returns false.

In the user defined method we are using two methods of the Math class, sqrt() method and floor() method. The Math.sqrt() method finds the square root of the given number and the floor() method finds the closest integer of the square root value returned by sqrt() method. Later we have calculated the difference between these two to check whether the difference is zero or non-zero. For a perfect square number this difference should be zero as the square root of perfect square number is integer itself.

package com.beginnersbook;
import java.util.Scanner;
class JavaExample { 

    static boolean checkPerfectSquare(double x)  
    { 

	// finding the square root of given number 
	double sq = Math.sqrt(x); 

	/* Math.floor() returns closest integer value, for
	 * example Math.floor of 984.1 is 984, so if the value
	 * of sq is non integer than the below expression would
	 * be non-zero.
	 */
	return ((sq - Math.floor(sq)) == 0); 
    } 
 
    public static void main(String[] args)  
    { 
	System.out.print("Enter any number:");
	Scanner scanner = new Scanner(System.in);
	double num = scanner.nextDouble(); 
	scanner.close();

	if (checkPerfectSquare(num)) 
		System.out.print(num+ " is a perfect square number"); 
	else
		System.out.print(num+ " is not a perfect square number"); 
    } 
}

Output:
Java Program to check perfect square

Related Java Examples

1. Java program to break integer into digits
2. Java program to check Armstrong number
3. Java program to check prime number
4. Java program to check leap year

Top Related Articles:

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

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

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