Founding Batch Offer · HLD · LLD · System Design at ₹799 until 30 Nov

Check Automorphic Number in Python

Does 25 appear at the end of its square? Answer with Python multiplication and endswith in a beginner exercise that separates numeric value from digit text.

BeginnerConditional ProgramsExample 11 of 20
check-automorphic-number.py
1# Program to check automorphic number
2
3num = int(input("Enter an integer: "))
4
5square = num * num
6if str(square).endswith(str(num)):
7 print(num, "is an automorphic number")
8else:
9 print(num, "is not an automorphic number")

Output

25 is an automorphic number

The text 625 ends with the text 25, so the condition is True.

In this one

  • Compute a square with multiplication
  • Compare decimal suffixes
  • Separate integer values from typed formatting

How it works

  1. 1

    Convert the input into num and calculate square.

  2. 2

    Convert square and num to strings for endswith.

  3. 3

    Print the automorphic message only if the suffix matches.

Edge cases

Zero

Enter 0.

The square text is 0 and ends with 0, so it qualifies.

Negative number

Enter -25.

625 cannot end with the signed text -25, so it fails.

What's going on

25 squared is 625. The final digits are still 25, so 25 is automorphic. This property concerns the written suffix of the square, not a digit sum.

square = num * num calculates the numeric square first. str converts both values to decimal text. endswith then asks whether the square's text ends in the original number's text, returning a Boolean for the if condition.

Zero and one qualify. Negative inputs do not qualify here because squaring removes the sign but str(num) still includes it. Leading zeroes from the typed response are lost by int conversion, which is appropriate for an integer property but not for comparing fixed-width identifier strings.

After this program

If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why does 25 qualify?

Pick an answer to check it.

Question 2

What does endswith return?

Pick an answer to check it.

Questions people ask

Why does 25 qualify?

Its square is 625, whose final decimal digits are 25.

What does endswith return?

It returns True or False depending on whether the suffix matches.

Are leading input zeroes preserved?

No. int stores a numeric value, so entering 025 is treated as 25.