ISC Practicals and Theory - Automorphic Number
Updated 21 Oct 2026
An automorphic number is a number whose square ends with the number itself. This program is written in the main method alone: it reads the number, counts its digits, finds the square and compares the last digits, all in one place.
Definition
A number $N$ with $k$ digits is an automorphic number if the last $k$ digits of $N^2$ are the same as $N$.
- $25$ has 2 digits and $25^2 = 625$. The last two digits of 625 are 25, so 25 is an automorphic number.
- $76^2 = 5776$ ends in 76, so 76 is automorphic. Also $5^2 = 25$ and $6^2 = 36$ end in 5 and 6.
- $12^2 = 144$ ends in 44, not 12, so 12 is not an automorphic number.
The last $k$ digits of a number are its remainder on division by $10^k$. So $N$ is automorphic when $N^2 \% 10^k = N$.
Algorithm
- Accept the number
nusingScanner. Ifn <= 0, display an invalid input message and stop. - Copy
ntotempand count its digits: whiletemp > 0, add 1 todigitsand dotemp = temp / 10. - Find
square = (long) n * n. Thelongtype is used because the square of a largeintdoes not fit in anint. - Start with
mod = 1and multiply it by 10 exactlydigitstimes, so thatmodbecomes $10^{digits}$. - If
square % mod == n, the last digits of the square are the number itself: display thatnis an Automorphic number. Otherwise display that it is not.
Program
Everything is written inside main: the input, the digit count, the square, the power of 10 and the comparison.
import java.util.Scanner;
public class Automorphic
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads input from the keyboard
System.out.print("Enter a number: ");
int n = sc.nextInt(); // the number to be checked
if (n <= 0)
{
System.out.println("Invalid input. Enter a positive number.");
}
else
{
int temp = n, digits = 0; // digits counts the digits of n
while (temp > 0)
{
digits++;
temp = temp / 10; // removes the last digit
}
long square = (long) n * n; // long: the square of a large int does not fit in an int
long mod = 1;
for (int i = 1; i <= digits; i++)
{
mod = mod * 10; // mod becomes 10 raised to digits
}
if (square % mod == n) // last digits of the square equal n
System.out.println(n + " is an Automorphic number as its square is " + square + ".");
else
System.out.println(n + " is not an Automorphic number as its square is " + square + ".");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the number typed by the user |
n | int | The number to be checked |
temp | int | Working copy of n whose last digit is removed in each pass of the counting loop |
digits | int | Number of digits of n |
square | long | The square of n |
mod | long | The value $10^{digits}$, built by repeated multiplication by 10 |
i | int | Loop counter of the for loop that builds mod |
Sample input and output
Sample 1
Enter a number: 76
76 is an Automorphic number as its square is 5776.Sample 2
Enter a number: 25
25 is an Automorphic number as its square is 625.Sample 3
Enter a number: 12
12 is not an Automorphic number as its square is 144.Sample 4
Enter a number: 0
Invalid input. Enter a positive number.Dry run
Take n = 76. The first loop counts the digits, so digits = 2. Then the for loop builds mod.
| Step | i | mod |
|---|---|---|
| Before the loop | - | 1 |
| Pass 1 | 1 | 10 |
| Pass 2 | 2 | 100 |
The loop stops as i becomes 3, which is more than digits. square is $76 \times 76 = 5776$, and 5776 % 100 is 76. This equals n, so the program prints that 76 is an Automorphic number as its square is 5776.
Variations
- Automorphic numbers in a range "Accept two numbers m and n and display all the automorphic numbers between m and n."
- Automorphic numbers up to a limit "Display all the automorphic numbers from 1 to 1000."
- Using a method that returns boolean "Write a method that accepts an integer and returns true if it is an automorphic number, otherwise false."
- Using recursive technique "Write a program to check whether a number is automorphic using a recursive method to count its digits."