Check if given number is perfect square

Given a number, check if it is a perfect square or not. **Examples :**

Input :2500Output :YesExplanation:2500 is a perfect square. 50 * 50 = 2500Input :2555Output :No

**Approach:**

- Take the square root of the number.
- Multiply the square root twice
- Use boolean equal operator to verify if the product of square root is equal to the number given.

## 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.` ` ` `if` `(x >= 0) {` ` ` `long` `long` `sr = ` `sqrt` `(x);` ` ` ` ` `// if product of square root ` ` ` `//is equal, then` ` ` `// return T/F` ` ` `return` `(sr * sr == x);` ` ` `}` ` ` `// else return false if n<0` ` ` `return` `false` `;` `}` `int` `main()` `{` ` ` `long` `long` `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)` ` ` `{` ` ` `if` `(x >= ` `0` `) {` ` ` ` ` `// Find floating point value of` ` ` `// square root of x.` ` ` `double` `sr = Math.sqrt(x);` ` ` ` ` `// if product of square root ` ` ` `// is equal, then` ` ` `// return T/F` ` ` `return` `((sr * sr) == x);` ` ` `}` ` ` `return` `false` `;` ` ` `}` ` ` `// 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):` ` ` `#if x >= 0, ` ` ` `if` `(x >` `=` `0` `):` ` ` `sr ` `=` `math.sqrt(x)` ` ` ` ` `#return boolean T/F` ` ` `return` `((sr` `*` `sr) ` `=` `=` `x)` ` ` `return` `false` `# Driver code` `x ` `=` `2500` `if` `(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.` ` ` `if` `(x >= 0) {` ` ` `double` `sr = Math.Sqrt(x);` ` ` ` ` `// if product of square root ` ` ` `// is equal, then` ` ` `// return T/F` ` ` `return` `(sr * sr == x);` ` ` `}` ` ` `// else return false if n<0` ` ` `return` `false` `;` ` ` `}` ` ` `// 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 the 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.