Perfect Square Verification

Category: Python Basics

Write a program that checks whether a number is a perfect square. The program should take an integer as input and output a Boolean indicating whether or not the input is a perfect square.


Input: 16
Output: True

Input: 15
Output: False

A perfect square is an integer that is the square of an integer. For example, 1, 4, 9, and 16 are perfect squares, but 3 and 11 are not. You can check if a number is a perfect square by calculating its square root and checking if it is an integer.

import math

def is_perfect_square(n):
root = math.sqrt(n)
return root.is_integer()

print(is_perfect_square(16)) # Outputs: True
print(is_perfect_square(15)) # Outputs: False

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!