# Check if given number is perfect square

Given a number n, check if it is a perfect square or not.

Examples :

Input : n = 2500
Output : Yes
Explanation: 2500 is a perfect square of 50

Input : n = 2555
Output : No

Recommended Practice

Approach:

• Take the floor()ed 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 ``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 = 2502;``    ``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(``int` `x)``    ``{``        ``if` `(x >= ``0``) {``          ` `            ``// Find floating point value of``            ``// square root of x.``            ``int` `sr = (``int``)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)``    ``{``        ``int` `x = ``2502``;` `        ``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):` `    ``#if x >= 0, ``    ``if``(x >``=` `0``):``        ``sr ``=` `int``(math.sqrt(x))``        ``# sqrt function returns floating value so we have to convert it into integer``        ``#return boolean T/F``        ``return` `((sr``*``sr) ``=``=` `x)``    ``return` `false` `# Driver code`  `x ``=` `2502``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.``        ``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 = 2502;` `        ``if` `(isPerfectSquare(x))``            ``Console.WriteLine(``"Yes"``);``        ``else``            ``Console.WriteLine(``"No"``);``    ``}``}` `// This code is contributed by vt_m.`

## Javascript

 ``

## PHP

 ``

Output
`No`

Time Complexity: O(log(x))
Auxiliary Space: O(1)

## Check if given number is perfect square using ceil, floor and sqrt() function.

• Use the floor and ceil and sqrt() function.
• If they are equal that implies the number is a perfect square.

## C++

 `// C++ program for the above approach``#include ``#include ``using` `namespace` `std;` `void` `checkperfectsquare(``int` `n)``{``    ` `    ``// If ceil and floor are equal ``    ``// the number is a perfect``    ``// square``    ``if` `(``ceil``((``double``)``sqrt``(n)) == ``floor``((``double``)``sqrt``(n))) {``        ``cout << ``"perfect square"``;``    ``}``    ``else` `{``        ``cout << ``"not a perfect square"``;``    ``}``}` `// Driver Code``int` `main()``{` `    ``int` `n = 49;``    ``checkperfectsquare(n);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;` `class` `GFG{` `static` `void` `checkperfectsquare(``int` `n)``{``    ` `    ``// If ceil and floor are equal``    ``// the number is a perfect``    ``// square``    ``if` `(Math.ceil((``double``)Math.sqrt(n)) ==``        ``Math.floor((``double``)Math.sqrt(n))) ``    ``{``        ``System.out.print(``"perfect square"``);``    ``}``    ``else``    ``{``        ``System.out.print(``"not a perfect square"``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `n = ``49``;``    ` `    ``checkperfectsquare(n);``}``}` `// This code is contributed by subhammahato348`

## Python3

 `# Python3 program for the above approach``import` `math` `def` `checkperfectsquare(x):``    ` `    ``# If ceil and floor are equal``    ``# the number is a perfect``    ``# square``    ``if` `(math.ceil(math.sqrt(n)) ``=``=``       ``math.floor(math.sqrt(n))):``        ``print``(``"perfect square"``)``    ``else``:``        ``print``(``"not a perfect square"``)``    ` `# Driver code``n ``=` `49`` ` `checkperfectsquare(n)` `# This code is contributed by jana_sayantan`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `static` `void` `checkperfectsquare(``int` `n)``{``    ` `    ``// If ceil and floor are equal``    ``// the number is a perfect``    ``// square``    ``if` `(Math.Ceiling((``double``)Math.Sqrt(n)) == ``        ``Math.Floor((``double``)Math.Sqrt(n)))``    ``{``        ``Console.Write(``"perfect square"``);``    ``}``    ``else``    ``{``        ``Console.Write(``"not a perfect square"``);``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `n = 49;` `    ``checkperfectsquare(n);``}``}` `// This code is contributed by subhammahato348`

## Javascript

 ``

Output
`perfect square`

Time Complexity : O(sqrt(n))
Auxiliary space: O(1)

## Check if given number is perfect square using Binary search:

Below is the implementation of the above approach:

## C++14

 `#include ``using` `namespace` `std;` `// Function to check if a number is a perfect square using``// binary search``bool` `isPerfectSquare(``int` `n)``{``    ``// Base case: 0 and 1 are perfect squares``    ``if` `(n <= 1) {``        ``return` `true``;``    ``}` `    ``// Initialize boundaries for binary search``    ``long` `long` `left = 1, right = n;` `    ``while` `(left <= right) {` `        ``// Calculate middle value``        ``long` `long` `mid = left + (right - left) / 2;` `        ``// Calculate square of the middle value``        ``long` `long` `square = mid * mid;` `        ``// If the square matches n, n is a perfect square``        ``if` `(square == n) {``            ``return` `true``;``        ``}` `        ``// If the square is smaller than n, search the right``        ``// half``        ``else` `if` `(square < n) {` `            ``left = mid + 1;``        ``}` `        ``// If the square is larger than n, search the left``        ``// half``        ``else` `{` `            ``right = mid - 1;``        ``}``    ``}` `    ``// If the loop completes without finding a perfect``    ``// square, n is not a perfect square``    ``return` `false``;``}` `int` `main()``{``    ``int` `n = 2500;` `    ``if` `(isPerfectSquare(n)) {``        ``cout << n << ``" is a perfect square."` `<< endl;``    ``}``    ``else` `{``        ``cout << n << ``" is not a perfect square."``             ``<< std::endl;``    ``}` `    ``return` `0;``}`

## Java

 `public` `class` `PerfectSquareCheck {``    ``// Function to check if a number is a perfect square``    ``// using binary search``    ``static` `boolean` `isPerfectSquare(``int` `n)``    ``{``        ``// Base case: 0 and 1 are perfect squares``        ``if` `(n <= ``1``) {``            ``return` `true``;``        ``}` `        ``// Initialize boundaries for binary search``        ``long` `left = ``1``, right = n;` `        ``while` `(left <= right) {``            ``// Calculate middle value``            ``long` `mid = left + (right - left) / ``2``;` `            ``// Calculate square of the middle value``            ``long` `square = mid * mid;` `            ``// If the square matches n, n is a perfect``            ``// square``            ``if` `(square == n) {``                ``return` `true``;``            ``}``            ``// If the square is smaller than n, search the``            ``// right half``            ``else` `if` `(square < n) {``                ``left = mid + ``1``;``            ``}``            ``// If the square is larger than n, search the``            ``// left half``            ``else` `{``                ``right = mid - ``1``;``            ``}``        ``}` `        ``// If the loop completes without finding a perfect``        ``// square, n is not a perfect square``        ``return` `false``;``    ``}` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``2500``;` `        ``if` `(isPerfectSquare(n)) {``            ``System.out.println(n + ``" is a perfect square."``);``        ``}``        ``else` `{``            ``System.out.println(``                ``n + ``" is not a perfect square."``);``        ``}``    ``}``}`

## Python

 `# Function to check if a number is a perfect square using binary search``def` `isPerfectSquare(n):``    ``# Base case: 0 and 1 are perfect squares``    ``if` `n <``=` `1``:``        ``return` `True` `    ``# Initialize boundaries for binary search``    ``left, right ``=` `1``, n` `    ``while` `left <``=` `right:``        ``# Calculate middle value``        ``mid ``=` `left ``+` `(right ``-` `left) ``/``/` `2` `        ``# Calculate square of the middle value``        ``square ``=` `mid ``*` `mid` `        ``# If the square matches n, n is a perfect square``        ``if` `square ``=``=` `n:``            ``return` `True``        ``# If the square is smaller than n, search the right half``        ``elif` `square < n:``            ``left ``=` `mid ``+` `1``        ``# If the square is larger than n, search the left half``        ``else``:``            ``right ``=` `mid ``-` `1` `    ``# If the loop completes without finding a perfect square, n is not a perfect square``    ``return` `False`  `n ``=` `2500``if` `isPerfectSquare(n):``    ``print``(n, ``"is a perfect square."``)``else``:``    ``print``(n, ``"is not a perfect square."``)`

## C#

 `using` `System;` `class` `Program``{``    ``// Function to check if a number is a perfect square using binary search``    ``static` `bool` `IsPerfectSquare(``int` `n)``    ``{``        ``// Base case: 0 and 1 are perfect squares``        ``if` `(n <= 1)``        ``{``            ``return` `true``;``        ``}` `        ``// Initialize boundaries for binary search``        ``long` `left = 1, right = n;` `        ``while` `(left <= right)``        ``{``            ``// Calculate middle value``            ``long` `mid = left + (right - left) / 2;` `            ``// Calculate square of the middle value``            ``long` `square = mid * mid;` `            ``// If the square matches n, n is a perfect square``            ``if` `(square == n)``            ``{``                ``return` `true``;``            ``}``            ``// If the square is smaller than n, search the right half``            ``else` `if` `(square < n)``            ``{``                ``left = mid + 1;``            ``}``            ``// If the square is larger than n, search the left half``            ``else``            ``{``                ``right = mid - 1;``            ``}``        ``}` `        ``// If the loop completes without finding a perfect square, n is not a perfect square``        ``return` `false``;``    ``}` `    ``static` `void` `Main(``string``[] args)``    ``{``        ``int` `n = 2500;` `        ``if` `(IsPerfectSquare(n))``        ``{``            ``Console.WriteLine(n + ``" is a perfect square."``);``        ``}``        ``else``        ``{``            ``Console.WriteLine(n + ``" is not a perfect square."``);``        ``}``    ``}``}`

## Javascript

 `// Function to check if a number is a perfect square using binary search``function` `isPerfectSquare(n) {``    ``// Base case: 0 and 1 are perfect squares``    ``if` `(n <= 1) {``        ``return` `true``;``    ``}` `    ``// Initialize boundaries for binary search``    ``let left = 1, right = n;` `    ``while` `(left <= right) {``        ``// Calculate middle value``        ``let mid = Math.floor(left + (right - left) / 2);` `        ``// Calculate square of the middle value``        ``let square = mid * mid;` `        ``// If the square matches n, n is a perfect square``        ``if` `(square === n) {``            ``return` `true``;``        ``}``        ``// If the square is smaller than n, search the right half``        ``else` `if` `(square < n) {``            ``left = mid + 1;``        ``}``        ``// If the square is larger than n, search the left half``        ``else` `{``            ``right = mid - 1;``        ``}``    ``}` `    ``// If the loop completes without finding a perfect square, n is not a perfect square``    ``return` `false``;``}` `const n = 2500;` `if` `(isPerfectSquare(n)) {``    ``console.log(n + ``" is a perfect square."``);``} ``else` `{``    ``console.log(n + ``" is not a perfect square."``);``}`

Output
```2500 is a perfect square.
```

Time Complexity: O(log n)
Auxiliary Space: O(1)

Previous
Next