Given a number, check if it is perfect square or not.
Examples :
Input : 2500 Output : Yes 2500 is a perfect square. 50 * 50 = 2500 Input : 2555 Output : No
- Take the square root of the number.
- Take floor/ceil/round of the square root which we got in step 1.
- Subtract value we got in step 2 from the square root.
- If the output of step 3 is 0 then the number is perfect square else not.
C++
// CPP program to find if x is a // perfect square. #include <bits/stdc++.h> using namespace std; bool isPerfectSquare(long double x) { // Find floating point value of // square root of x. long double sr = sqrt(x); // If square root is an integer return ((sr - floor(sr)) == 0); } int main() { long double x = 2500; if (isPerfectSquare(x)) cout << "Yes"; else cout << "No"; return 0; } |
chevron_right
filter_none
Java
// Java program to find if x is a // perfect square. class GFG { static boolean isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0); } // Driver code public static void main(String[] args) { double x = 2500; if (isPerfectSquare(x)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by Anant Agarwal. |
chevron_right
filter_none
Python3
# Python program to find if x is a # perfect square. import math def isPerfectSquare(x): # Find floating point value of # square root of x. sr = math.sqrt(x) # If square root is an integer return ((sr - math.floor(sr)) == 0) # Driver code x = 2500if (isPerfectSquare(x)): print("Yes") else: print("No") # This code is contributed # by Anant Agarwal. |
chevron_right
filter_none
C#
// C# program to find if x is a // perfect square. using System; class GFG { static bool isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.Sqrt(x); // If square root is an integer return ((sr - Math.Floor(sr)) == 0); } // Driver code public static void Main() { double x = 2500; if (isPerfectSquare(x)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP program to find if x is // a perfect square. function isPerfectSquare($x) { // Find floating point value // of square root of x. $sr = sqrt($x); // If square root is an integer return (($sr - floor($sr)) == 0); } // Driver code $x = 2500; if (isPerfectSquare($x)) echo("Yes"); else echo("No"); // This code is contributed by Ajit. ?> |
chevron_right
filter_none
Output:
Yes
To know more about inbuilt sqrt function, refer this Stackoverflow and this StackExchange threads.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if a number is a perfect square having all its digits as a perfect square
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a number is perfect square without finding square root
- Count numbers upto N which are both perfect square and perfect cube
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check if a given number is a Perfect square using Binary Search
- Check if the sum of a subarray within a given range is a perfect square or not
- Check whether the number can be made perfect square after adding 1
- Check whether the number can be made perfect square after adding K
- Find the Next perfect square greater than a given number
- Largest factor of a given number which is a perfect square
- Probability of getting a perfect square when a random number is chosen in a given range
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Previous perfect square and cube number smaller than number N
- Check perfect square using addition/subtraction
- Check if product of array containing prime numbers is a perfect square
- Maximum subsequence sum from a given array which is a perfect square
- Count perfect square fractions from given array
- Minimize increments or decrements by 2 to convert given value to a perfect square
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t

