SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Automorphic Number Program in Java and Python

23 February 2014

Automorphic Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to input a number and check whether it is an Automorphic Number or not.

Automorphic Number: An automorphic number is a number which is present at the end of its square.

Example: 25 is an Automorphic Number because 25² = 625 and 625 ends with 25. 6 is an Automorphic Number because 6² = 36 and 36 ends with 6. 9 is not an Automorphic Number because 9² = 81 and 81 does not end with 9.

Algorithm:

Step 1: Start.

Step 2: Accept a number from the user.

Step 3: Find the square of the number.

Step 4: Convert the number and its square into strings.

Step 5: Check whether the square string ends with the number string.

Step 6: If it does, display that the number is an Automorphic Number; otherwise, display that it is not an Automorphic Number.

Step 7: Store the number of digits of the original number before comparing with the square.

Step 8: Extract only the ending digits of the square that match this digit count.

Step 9: Compare the extracted ending part with the original number.

Step 10: Stop.

Explanation:

The program checks whether a number is automorphic by comparing the ending part of its square with the number itself. An automorphic number is not identified by the full square, but by the last digits of the square. For example, 25 is automorphic because 25 × 25 = 625, and the square ends with 25.

The Java solution first calculates square = n * n. Instead of extracting digits numerically, it converts both the original number and the square into strings. This makes the ending comparison direct and easy to understand. The string version of the number tells exactly what ending sequence must be present in the square. The method endsWith() then checks whether squareString finishes with numberString.

This approach is useful for students because it avoids a longer loop for counting digits and finding powers of 10. Conceptually, however, it is doing the same thing: it is checking only the last few digits of the square, where the number of digits checked is equal to the number of digits in the original number. If the ending matches, the number is automorphic; otherwise it is not. The original number is kept unchanged throughout, so the final output can clearly mention the entered value.

Java Program:

Java
/**
* The class Automorphic accepts a number and checks whether
* it is an Automorphic Number using string comparison.
*
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class Automorphic
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        // The square is needed because an automorphic number appears at its end.
        int square = n * n;

        /*
        * Convert both values to strings so that the ending part
        * of the square can be checked directly.
        */
        String numberString = Integer.toString(n);
        String squareString = Integer.toString(square);

        if(squareString.endsWith(numberString))
        System.out.println(n + " is an Automorphic Number");
        else
        System.out.println(n + " is not an Automorphic Number");
    }
}

Alternate Java Program Without Using Strings:

Java
/**
* Alternate method to check Automorphic Number without using strings.
*/

import java.util.Scanner;

class Automorphic
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        int square = n * n;
        int copy = n;
        int digits = 0;

        /*
        * Count the number of digits in the original number.
        */
        while(copy > 0)
        {
            digits++;
            copy = copy / 10;
        }

        /*
        * If n has 2 digits, extract the last 2 digits of square.
        * If n has 3 digits, extract the last 3 digits of square.
        */
        int end = square % (int)Math.pow(10, digits);

        if(end == n)
        System.out.println(n + " is an Automorphic Number");
        else
        System.out.println(n + " is not an Automorphic Number");
    }
}

Equivalent Python Program:

Python
# Read the number and keep any required copy for digit or divisor processing.
# Loops and conditions implement the number-property test step by step.
# Display the result according to the flag/counter/calculated value.

n = int(input("Enter a number: "))

# The square is needed because an automorphic number appears at its end.
square = n * n

# Convert both values to strings for easy ending comparison.
number_string = str(n)
square_string = str(square)

if square_string.endswith(number_string):
    print(n, "is an Automorphic Number")
else:
    print(n, "is not an Automorphic Number")

Output:

Enter a number: 25 25 is an Automorphic Number Enter a number: 6 6 is an Automorphic Number Enter a number: 9 9 is not an Automorphic Number

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →