Related Articles
Check if given number is perfect square
• Difficulty Level : Basic
• Last Updated : 27 Nov, 2018

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

1. Take the square root of the number.
2. Take floor/ceil/round of the square root which we got in step 1.
3. Subtract value we got in step 2 from the square root.
4. 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 ` `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; ` `} `

## 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. `

## 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 ``=` `2500` `if` `(isPerfectSquare(x)): ` `    ``print``(``"Yes"``) ` `else``: ` `    ``print``(``"No"``) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

## 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. `

## PHP

 ` `

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :