An Automorphic number is a non-negative integer n whose square n * n ends with the exact digits of n itself—such as 5 (25), 25 (625), 76 (5776), and 376 (141376). In Java BlueJ, compute `long square = (long) num * num;` to avoid integer overflow, then compare trailing digits simultaneously inside `while (temp > 0)` by testing `temp % 10 != square % 10` and dividing both `temp` and `square` by 10 each step.
- 1. How Suffix Matching Defines Automorphic Numbers
- 2. Integer Digit-Sync Loop vs Power-of-Ten Modulo
- 3. Dry Run Execution on Input 76 and 12
- 4. AutomorphicNumber.java (ICSE BlueJ Program)
- 5. Variable Description Table (VDT)
- 6. Common Mistakes & Top Scorer Tips
- 7. Interactive Concept Check Quiz
- 8. Frequently Asked Questions
1. How Suffix Matching Defines Automorphic Numbers
Suffix matching sits at the heart of every Automorphic number test. A non-negative integer n is automorphic whenever the rightmost d digits of n squared match the d digits of n in exact order, where d is the digit length of n. Small examples appear frequently in ICSE short-answer questions: 5 squared equals 25 (ends in 5), 6 squared equals 36 (ends in 6), and 25 squared equals 625 (ends in 25). Larger two- and three-digit examples such as 76 (square 5776) and 376 (square 141376) reveal why simply checking the last single digit is insufficient—every digit from right to left must match until the original number is exhausted. Negative integers cannot be automorphic because squares of real integers are always non-negative and lack a minus sign.
- Every automorphic number other than 0 and 1 must end in 5 or 6 in base 10.
- Trailing-digit equality must hold across all digits of n, not only the units place.
- Negative numbers fail immediately since n * n >= 0 cannot match a negative value.
2. Integer Digit-Sync Loop vs Power-of-Ten Modulo
Two clean integer algorithms exist in BlueJ for testing suffix equality without relying on `String.endsWith()` or floating-point `Math.pow()`. The first computes a `divisor` equal to `10^d` by multiplying `divisor *= 10` once per digit of `num`, then tests `square % divisor == num`. The second—shown in our program—compares `temp % 10` directly against `square % 10` inside `while (temp > 0)`, dividing both `temp` and `square` by 10 after each match and breaking immediately on any mismatch. Digit-by-digit synchronization handles `num = 0` naturally (since `isAutomorphic` starts `true` and the loop skips when `temp == 0`), avoids building a large divisor variable, and terminates on the very first non-matching trailing digit for non-automorphic inputs like 12 or 45.
- Digit-sync loop exits early on the first mismatch via `break;`.
- Casting `(long) num * num` prevents 32-bit overflow when squaring 5-digit inputs like 90625.
- Keeping `originalSquare` separate lets you print the full square after `square` is divided down.
3. Dry Run Execution on Input 76 and 12
Walking through `num = 76` demonstrates how the synchronized loop validates suffix alignment. Before the loop starts, `square = 5776`, `originalSquare = 5776`, `temp = 76`, and `isAutomorphic = true`. In Iteration 1 (`temp = 76 > 0`), `temp % 10 = 6` and `square % 10 = 6`. Because `6 == 6`, both divide by 10: `temp = 7` and `square = 577`. In Iteration 2 (`temp = 7 > 0`), `temp % 10 = 7` and `square % 10 = 7`. Both match again, updating `temp = 0` and `square = 57`. The condition `temp > 0` now fails, leaving `isAutomorphic` as `true` and printing `76 is an Automorphic number (Square = 5776).` Contrast this with `num = 12` (`square = 144`): in Iteration 1, `12 % 10 = 2` while `144 % 10 = 4`, so `2 != 4` sets `isAutomorphic = false` and breaks immediately.
- Trace 76 -> Iteration 1: 6 == 6 (temp=7, square=577); Iteration 2: 7 == 7 (temp=0, square=57).
- Trace 12 -> Iteration 1: 2 != 4 -> sets isAutomorphic=false and breaks in 1 step.
- Trace 0 -> loop condition 0 > 0 is false -> isAutomorphic remains true.
4. AutomorphicNumber.java (ICSE BlueJ Program)
import java.util.Scanner;
public class AutomorphicNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
int num = sc.nextInt();
if (num < 0) {
System.out.println(num + " is NOT an Automorphic number.");
sc.close();
return;
}
long square = (long) num * num;
long originalSquare = square;
int temp = num;
boolean isAutomorphic = true;
while (temp > 0) {
if (temp % 10 != square % 10) {
isAutomorphic = false;
break;
}
temp = temp / 10;
square = square / 10;
}
if (isAutomorphic) {
System.out.println(num + " is an Automorphic number (Square = " + originalSquare + ").");
} else {
System.out.println(num + " is NOT an Automorphic number (Square = " + originalSquare + ").");
}
sc.close();
}
}
Verified Console / Dry-Run Output:
Enter a non-negative integer: 76
76 is an Automorphic number (Square = 5776).
Enter a non-negative integer: 12
12 is NOT an Automorphic number (Square = 144).
5. Variable Description Table (VDT)
| Variable Name | Data Type | Purpose in Program |
|---|---|---|
| sc | Scanner | Reads the integer entered by the user from System.in |
| num | int | Stores the original non-negative integer being tested |
| square | long | Holds num * num and shifts right by one digit per loop iteration |
| originalSquare | long | Preserves the full unshifted square for displaying in the final output |
| temp | int | Mutable copy of num consumed from right to left during digit comparison |
| isAutomorphic | boolean | Flag tracking whether all trailing digits of the square matched num |
Get 1-on-1 BlueJ Practice for ICSE Number Programs
Build rock-solid loop logic and BlueJ exam confidence with IIT/NIT mentors at 4Achievers Junior (Grades 3-12, 1-on-1 & 1:4 Micro-Pods).
6. Common Mistakes & Top Scorer Tips
- Avoid Mistake: Using `(int) Math.pow(10, count)` without accounting for double precision limits on large numbers, or forgetting to cast `Math.pow` back to `long` or `int`.
- Avoid Mistake: Computing `int square = num * num;` without casting to `long`, which overflows silently for 5-digit automorphic candidates like `90625` (`90625^2 = 8212890625`, exceeding `Integer.MAX_VALUE`).
- Avoid Mistake: Mutating `square` during the comparison loop and then printing the shortened `square` instead of preserving `originalSquare` for the output message.
- Examiner / Mentor Tip: Mention in your viva why `(long) num * num` is safer than `int square = num * num`, especially for inputs above 46340.
- Examiner / Mentor Tip: Note that if the ICSE question asks to use `String` methods, `String.valueOf(square).endsWith(String.valueOf(num))` is valid, whereas pure numeric questions expect `%` and `/` loops.
- Examiner / Mentor Tip: Always include the `boolean` flag and explain its initial value in your Variable Description Table.
7. Interactive Concept Check Quiz
Question: Which of the following integers is an Automorphic number?
8. Frequently Asked Questions
Are 0 and 1 considered Automorphic numbers in Java?
Why does the program use long for square instead of int?
Can I also check an Automorphic number using modulus with a power of 10?
Explore Related Lessons & Free Interactive Tools
- All 110 Guides in ICSE Java BlueJ Library
- Spy Number Program in Java (ICSE BlueJ Guide)
- Duck Number Program in Java Using Loop and String
- Pronic (Heteromecic) Number Program in Java (ICSE)
- Interactive Tool: ICSE Class 10 Java BlueJ Output Quiz
- Interactive Tool: ICSE Variable Description Table (VDT) Auto-Formatter
