Check if a number is the square of 6

EasyIf-Else Programs · 41 of 64

Write a Python program to check whether a number is the square of 6, and in general whether it is a perfect square.

Advertisement
Input
37, 36
Output
37 square of 6: False | perfect square: False
36 square of 6: True | perfect square: True

Approach

  1. Compare with 6 ** 2, and use math.isqrt for the general test.

Solution

Python
import math for n in [37, 36]:    root = math.isqrt(n)    print(n, "square of 6:", n == 6 ** 2, "| perfect square:", root * root == n)
Output
37 square of 6: False | perfect square: False
36 square of 6: True | perfect square: True

Line by line

Line 4

isqrt gives the whole-number square root.

Line 5

A number is a perfect square when that root squared gives the number back.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Leave a Comment