Perfect Square in Python

Perfect square in python | In this post, we will develop a Python program to check whether the given number is a perfect square or not.

There are different ways to check the given number is a perfect square or not. You may say that the easiest to use sqrt() method. But it is not the only way to check the given number is a perfect square or not.

Example:-

Input: 100
Output: Yes
100 is a perfect square.
10 x 10 = 100

Input: 50
Output: No
50 is not a perfect square.

Python Program to Check Whether a Number is a Perfect Square

# Python program to check number is perfect square or not

import math  #importing math-module

# take inputs
num = int(input('Enter number: '))

root = math.sqrt(num)
# check number is perfecct square
if int(root + 0.5) ** 2 == num:
    print(num, 'is a perfect square')
else:
    print(num, 'is not a perfect square')

Output for the different input values:-

Enter number: 4
4 is a perfect square

Enter number: 5
5 is not a perfect square

Enter number: 100
100 is a perfect square

Check if Number is Perfect Square Python Program

# Python program to check number is perfect square or not

import math  #importing math-module

def PerfectSquare(x):   #user-defined function
    root = math.sqrt(x)
    # check number is perfecct square
    if int(root + 0.5) ** 2 == x:
        print(x, 'is a perfect square')
    else:
        print(x, 'is not a perfect square')
    return root

# take inputs
num = int(input('Enter number: '))

# calling function
PerfectSquare(num)

Output:-

Enter number: 50
50 is not a perfect square

Python Program without using sqrt() method

Check whether the given number is a perfect square or not without using the sqrt() method.

# Python program to check perfect square without sqrt()

def PerfectSquare(x) :
    i = 1
    while(i**2 <= x):
        if ((x%i == 0) and (x/i == i)):
            return True
        i += 1
    return False

# take inputs
num = int(input('Enter number: '))

# calling function and display result
if (PerfectSquare(num)):
    print(num, 'is a perfect square')
else:
    print(num, 'is not a perfect square')

Output:-

Enter number: 25
25 is a perfect square

Time complexity of this program is O(sqrt(N)).

In the below program, we will use binary search to find a number in range 1 to n whose square is equal to n. Therefore, each iteration reduces to half [1 to n/2-1 OR n/2 to n].

# Python program to check perfect square without sqrt()

def PerfectSquare(x):
    a = 1
    b = x
    while (a <= b):
        c = (a + b) >> 1
         
        # if c is perfect square
        if ((c**2) == x):
            return True
         
        # c is small -> go b to increase c
        if (c**2 < x):
            a = c + 1
           
        # c is large -> to a to decrease c
        else:
            b = c - 1
    return False
# take inputs
num = int(input('Enter number: '))

# calling function and display result
if (PerfectSquare(num)):
    print(num, 'is a perfect square')
else:
    print(num, 'is not a perfect square')

Output:-

Enter number: 10
10 is not a perfect square

Time complexity of this program is O(log(N)) and Space complexity is O(1).

Python Program to Find Perfect Square in Range

This program will be print all perfect squares from the given range.

# Python program to print perfect square in range

def PerfectSquare(a, b):
    for i in range(a, b + 1): 
        if (i**(.5) == int(i**(.5))): 
            print(i, end=" ") 

# take inputs
start = int(input('Enter start number: '))
end = int(input('Enter end number: '))
print('All perfect square')

# calling function
PerfectSquare(start, end)

Output for the different input values:-

Enter start number: 1
Enter end number: 50
All perfect square
1 4 9 16 25 36 49

Enter start number: 1
Enter end number: 500
All perfect square
1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484

Also See:- Python Program to Find Square root

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “Perfect Square in Python”

  1. Prakhar Chaubey


    import math
    num = int(input("Till what number you want perfect squares: "))
    for i in range(0, num + 1):
        root = math.sqrt(i)
        if int(root + 0.5)**2 == i:
            print(i)

Leave a Comment

Your email address will not be published. Required fields are marked *