Automorphic Number Check in Java: Explanation & Practice

Check if a number is automorphic

Problem summary

An automorphic number's square ends with the number itself (e.g., 25² = 625). Check if the given number is automorphic.

Starter code

public class Main {
    public static void main(String[] args) {
        int number = 25; // Test case 1
        
        // Check if automorphic
        // Print: Is automorphic: <true/false>
    }
}

Expected output and test cases

  • 25² = 625 ends with 25
    Is automorphic: true
  • 76² = 5776 ends with 76
    Is automorphic: true
  • 15² = 225 doesn't end with 15
    Is automorphic: false

Hints

  1. Calculate the square
  2. Find how many digits in original number
  3. Use modulus to get last n digits of square

Related Core Java Basics exercises

Practice all Core Java Basics exercises · Run this idea in the Java compiler