Check if a number is perfect square without finding square root

We need to check whether a number is a perfect square or not without finding its square root.

Examples:

Input : n = 36
Output : Yes

Input : n = 12
Output : No



We have discussed a method to check if a number is perfect square.

The idea is to run a loop from i = 1 to floor(sqrt(n)) then check if squaring it makes n.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if a number is perfect 
// square without finding square root
#include <bits/stdc++.h> 
using namespace std; 
  
bool isPerfectSquare(int n) 
    long int divisorcount = 0; 
    for (int i = 1; i * i <= n; i++) 
        if (n % i == 0) 
            divisorcount++; 
  
    // Return true if divisorcount is odd 
    return (divisorcount & 1);     
      
//Driver code
int main() 
    long long int n = 36; 
    if (isPerfectSquare(n)) 
        cout << "Yes";     
    else
        cout << "No"
      
    return 0; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if a number is perfect  
// square without finding the square root 
public class GfG{
  
    static boolean isPerfectSquare(int n)  
    {  
        int divisorcount = 0;  
        for (int i = 1; i * i <= n; i++)  
            if (n % i == 0)  
                divisorcount++;  
        
        // Return true if divisorcount is odd  
        return (divisorcount & 1) == 1;      
    }  
      
    public static void main(String []args){
          
        int n = 36;
          
        if (isPerfectSquare(n))  
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by Rituraj Jain

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check if a number is 
# perfect square without finding square root 
  
# from math import sqrt function
from math import sqrt
  
def isPerfectSquare(n) :
      
    divisorcount = 0
    for i in range(1, int(sqrt(n)) + 1) :
        if (n % i == 0) :
            divisorcount += 1
              
    # Return true if divisorcount is odd 
    return (divisorcount and 1)
  
# Driver code 
if __name__ == "__main__"
  
    n = 36
    if (isPerfectSquare(n)) :
        print("Yes")
    else :
        print("No")
      
# This code is contributed by Ryuga

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

      
// C# program to check if a number is perfect  
// square without finding the square root 
using System;
  
public class GfG{
   
    static bool isPerfectSquare(int n)  
    {  
        int divisorcount = 0;  
        for (int i = 1; i * i <= n; i++)  
            if (n % i == 0)  
                divisorcount++;  
         
        // Return true if divisorcount is odd  
        return (divisorcount & 1) == 1;      
    }  
       
    public static void Main(){
           
        int n = 36;
           
        if (isPerfectSquare(n))  
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
/*This code is contributed by Rajput-Ji*/

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if a number is perfect 
// square without finding square root
  
function isPerfectSquare($n
    $divisorcount = 0; 
    for ($i = 1; $i * $i <= $n; $i++) 
        if ($n % $i == 0) 
            $divisorcount++; 
  
    // Return true if divisorcount is odd 
    return ($divisorcount & 1);     
      
// Driver code
$n = 36; 
if (isPerfectSquare($n)) 
    echo "Yes";     
else
    echo "No"
  
// This code is contributed
// by Akanksha Rai

chevron_right



Output:

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.